spi-omap2-mcspi.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OMAP2 McSPI controller driver
  4. *
  5. * Copyright (C) 2005, 2006 Nokia Corporation
  6. * Author: Samuel Ortiz <[email protected]> and
  7. * Juha Yrjola <[email protected]>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/device.h>
  13. #include <linux/delay.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/gcd.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/platform_data/spi-omap2-mcspi.h>
  28. #define OMAP2_MCSPI_MAX_FREQ 48000000
  29. #define OMAP2_MCSPI_MAX_DIVIDER 4096
  30. #define OMAP2_MCSPI_MAX_FIFODEPTH 64
  31. #define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
  32. #define SPI_AUTOSUSPEND_TIMEOUT 2000
  33. #define OMAP2_MCSPI_REVISION 0x00
  34. #define OMAP2_MCSPI_SYSSTATUS 0x14
  35. #define OMAP2_MCSPI_IRQSTATUS 0x18
  36. #define OMAP2_MCSPI_IRQENABLE 0x1c
  37. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  38. #define OMAP2_MCSPI_SYST 0x24
  39. #define OMAP2_MCSPI_MODULCTRL 0x28
  40. #define OMAP2_MCSPI_XFERLEVEL 0x7c
  41. /* per-channel banks, 0x14 bytes each, first is: */
  42. #define OMAP2_MCSPI_CHCONF0 0x2c
  43. #define OMAP2_MCSPI_CHSTAT0 0x30
  44. #define OMAP2_MCSPI_CHCTRL0 0x34
  45. #define OMAP2_MCSPI_TX0 0x38
  46. #define OMAP2_MCSPI_RX0 0x3c
  47. /* per-register bitmasks: */
  48. #define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
  49. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  50. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  51. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  52. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  53. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  54. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  55. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  56. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  57. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  58. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  59. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  60. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  61. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  62. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  63. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  64. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  65. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  66. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  67. #define OMAP2_MCSPI_CHCONF_FFET BIT(27)
  68. #define OMAP2_MCSPI_CHCONF_FFER BIT(28)
  69. #define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
  70. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  71. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  72. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  73. #define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
  74. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  75. #define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
  76. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  77. /* We have 2 DMA channels per CS, one for RX and one for TX */
  78. struct omap2_mcspi_dma {
  79. struct dma_chan *dma_tx;
  80. struct dma_chan *dma_rx;
  81. struct completion dma_tx_completion;
  82. struct completion dma_rx_completion;
  83. char dma_rx_ch_name[14];
  84. char dma_tx_ch_name[14];
  85. };
  86. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  87. * cache operations; better heuristics consider wordsize and bitrate.
  88. */
  89. #define DMA_MIN_BYTES 160
  90. /*
  91. * Used for context save and restore, structure members to be updated whenever
  92. * corresponding registers are modified.
  93. */
  94. struct omap2_mcspi_regs {
  95. u32 modulctrl;
  96. u32 wakeupenable;
  97. struct list_head cs;
  98. };
  99. struct omap2_mcspi {
  100. struct completion txdone;
  101. struct spi_master *master;
  102. /* Virtual base address of the controller */
  103. void __iomem *base;
  104. unsigned long phys;
  105. /* SPI1 has 4 channels, while SPI2 has 2 */
  106. struct omap2_mcspi_dma *dma_channels;
  107. struct device *dev;
  108. struct omap2_mcspi_regs ctx;
  109. int fifo_depth;
  110. bool slave_aborted;
  111. unsigned int pin_dir:1;
  112. size_t max_xfer_len;
  113. };
  114. struct omap2_mcspi_cs {
  115. void __iomem *base;
  116. unsigned long phys;
  117. int word_len;
  118. u16 mode;
  119. struct list_head node;
  120. /* Context save and restore shadow register */
  121. u32 chconf0, chctrl0;
  122. };
  123. static inline void mcspi_write_reg(struct spi_master *master,
  124. int idx, u32 val)
  125. {
  126. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  127. writel_relaxed(val, mcspi->base + idx);
  128. }
  129. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  130. {
  131. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  132. return readl_relaxed(mcspi->base + idx);
  133. }
  134. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  135. int idx, u32 val)
  136. {
  137. struct omap2_mcspi_cs *cs = spi->controller_state;
  138. writel_relaxed(val, cs->base + idx);
  139. }
  140. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  141. {
  142. struct omap2_mcspi_cs *cs = spi->controller_state;
  143. return readl_relaxed(cs->base + idx);
  144. }
  145. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  146. {
  147. struct omap2_mcspi_cs *cs = spi->controller_state;
  148. return cs->chconf0;
  149. }
  150. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  151. {
  152. struct omap2_mcspi_cs *cs = spi->controller_state;
  153. cs->chconf0 = val;
  154. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  155. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
  156. }
  157. static inline int mcspi_bytes_per_word(int word_len)
  158. {
  159. if (word_len <= 8)
  160. return 1;
  161. else if (word_len <= 16)
  162. return 2;
  163. else /* word_len <= 32 */
  164. return 4;
  165. }
  166. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  167. int is_read, int enable)
  168. {
  169. u32 l, rw;
  170. l = mcspi_cached_chconf0(spi);
  171. if (is_read) /* 1 is read, 0 write */
  172. rw = OMAP2_MCSPI_CHCONF_DMAR;
  173. else
  174. rw = OMAP2_MCSPI_CHCONF_DMAW;
  175. if (enable)
  176. l |= rw;
  177. else
  178. l &= ~rw;
  179. mcspi_write_chconf0(spi, l);
  180. }
  181. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  182. {
  183. struct omap2_mcspi_cs *cs = spi->controller_state;
  184. u32 l;
  185. l = cs->chctrl0;
  186. if (enable)
  187. l |= OMAP2_MCSPI_CHCTRL_EN;
  188. else
  189. l &= ~OMAP2_MCSPI_CHCTRL_EN;
  190. cs->chctrl0 = l;
  191. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  192. /* Flash post-writes */
  193. mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
  194. }
  195. static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
  196. {
  197. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  198. u32 l;
  199. /* The controller handles the inverted chip selects
  200. * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
  201. * the inversion from the core spi_set_cs function.
  202. */
  203. if (spi->mode & SPI_CS_HIGH)
  204. enable = !enable;
  205. if (spi->controller_state) {
  206. int err = pm_runtime_resume_and_get(mcspi->dev);
  207. if (err < 0) {
  208. dev_err(mcspi->dev, "failed to get sync: %d\n", err);
  209. return;
  210. }
  211. l = mcspi_cached_chconf0(spi);
  212. if (enable)
  213. l &= ~OMAP2_MCSPI_CHCONF_FORCE;
  214. else
  215. l |= OMAP2_MCSPI_CHCONF_FORCE;
  216. mcspi_write_chconf0(spi, l);
  217. pm_runtime_mark_last_busy(mcspi->dev);
  218. pm_runtime_put_autosuspend(mcspi->dev);
  219. }
  220. }
  221. static void omap2_mcspi_set_mode(struct spi_master *master)
  222. {
  223. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  224. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  225. u32 l;
  226. /*
  227. * Choose master or slave mode
  228. */
  229. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  230. l &= ~(OMAP2_MCSPI_MODULCTRL_STEST);
  231. if (spi_controller_is_slave(master)) {
  232. l |= (OMAP2_MCSPI_MODULCTRL_MS);
  233. } else {
  234. l &= ~(OMAP2_MCSPI_MODULCTRL_MS);
  235. l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  236. }
  237. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  238. ctx->modulctrl = l;
  239. }
  240. static void omap2_mcspi_set_fifo(const struct spi_device *spi,
  241. struct spi_transfer *t, int enable)
  242. {
  243. struct spi_master *master = spi->master;
  244. struct omap2_mcspi_cs *cs = spi->controller_state;
  245. struct omap2_mcspi *mcspi;
  246. unsigned int wcnt;
  247. int max_fifo_depth, bytes_per_word;
  248. u32 chconf, xferlevel;
  249. mcspi = spi_master_get_devdata(master);
  250. chconf = mcspi_cached_chconf0(spi);
  251. if (enable) {
  252. bytes_per_word = mcspi_bytes_per_word(cs->word_len);
  253. if (t->len % bytes_per_word != 0)
  254. goto disable_fifo;
  255. if (t->rx_buf != NULL && t->tx_buf != NULL)
  256. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
  257. else
  258. max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
  259. wcnt = t->len / bytes_per_word;
  260. if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
  261. goto disable_fifo;
  262. xferlevel = wcnt << 16;
  263. if (t->rx_buf != NULL) {
  264. chconf |= OMAP2_MCSPI_CHCONF_FFER;
  265. xferlevel |= (bytes_per_word - 1) << 8;
  266. }
  267. if (t->tx_buf != NULL) {
  268. chconf |= OMAP2_MCSPI_CHCONF_FFET;
  269. xferlevel |= bytes_per_word - 1;
  270. }
  271. mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
  272. mcspi_write_chconf0(spi, chconf);
  273. mcspi->fifo_depth = max_fifo_depth;
  274. return;
  275. }
  276. disable_fifo:
  277. if (t->rx_buf != NULL)
  278. chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
  279. if (t->tx_buf != NULL)
  280. chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
  281. mcspi_write_chconf0(spi, chconf);
  282. mcspi->fifo_depth = 0;
  283. }
  284. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  285. {
  286. unsigned long timeout;
  287. timeout = jiffies + msecs_to_jiffies(1000);
  288. while (!(readl_relaxed(reg) & bit)) {
  289. if (time_after(jiffies, timeout)) {
  290. if (!(readl_relaxed(reg) & bit))
  291. return -ETIMEDOUT;
  292. else
  293. return 0;
  294. }
  295. cpu_relax();
  296. }
  297. return 0;
  298. }
  299. static int mcspi_wait_for_completion(struct omap2_mcspi *mcspi,
  300. struct completion *x)
  301. {
  302. if (spi_controller_is_slave(mcspi->master)) {
  303. if (wait_for_completion_interruptible(x) ||
  304. mcspi->slave_aborted)
  305. return -EINTR;
  306. } else {
  307. wait_for_completion(x);
  308. }
  309. return 0;
  310. }
  311. static void omap2_mcspi_rx_callback(void *data)
  312. {
  313. struct spi_device *spi = data;
  314. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  315. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  316. /* We must disable the DMA RX request */
  317. omap2_mcspi_set_dma_req(spi, 1, 0);
  318. complete(&mcspi_dma->dma_rx_completion);
  319. }
  320. static void omap2_mcspi_tx_callback(void *data)
  321. {
  322. struct spi_device *spi = data;
  323. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  324. struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  325. /* We must disable the DMA TX request */
  326. omap2_mcspi_set_dma_req(spi, 0, 0);
  327. complete(&mcspi_dma->dma_tx_completion);
  328. }
  329. static void omap2_mcspi_tx_dma(struct spi_device *spi,
  330. struct spi_transfer *xfer,
  331. struct dma_slave_config cfg)
  332. {
  333. struct omap2_mcspi *mcspi;
  334. struct omap2_mcspi_dma *mcspi_dma;
  335. struct dma_async_tx_descriptor *tx;
  336. mcspi = spi_master_get_devdata(spi->master);
  337. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  338. dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
  339. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, xfer->tx_sg.sgl,
  340. xfer->tx_sg.nents,
  341. DMA_MEM_TO_DEV,
  342. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  343. if (tx) {
  344. tx->callback = omap2_mcspi_tx_callback;
  345. tx->callback_param = spi;
  346. dmaengine_submit(tx);
  347. } else {
  348. /* FIXME: fall back to PIO? */
  349. }
  350. dma_async_issue_pending(mcspi_dma->dma_tx);
  351. omap2_mcspi_set_dma_req(spi, 0, 1);
  352. }
  353. static unsigned
  354. omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
  355. struct dma_slave_config cfg,
  356. unsigned es)
  357. {
  358. struct omap2_mcspi *mcspi;
  359. struct omap2_mcspi_dma *mcspi_dma;
  360. unsigned int count, transfer_reduction = 0;
  361. struct scatterlist *sg_out[2];
  362. int nb_sizes = 0, out_mapped_nents[2], ret, x;
  363. size_t sizes[2];
  364. u32 l;
  365. int elements = 0;
  366. int word_len, element_count;
  367. struct omap2_mcspi_cs *cs = spi->controller_state;
  368. void __iomem *chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  369. struct dma_async_tx_descriptor *tx;
  370. mcspi = spi_master_get_devdata(spi->master);
  371. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  372. count = xfer->len;
  373. /*
  374. * In the "End-of-Transfer Procedure" section for DMA RX in OMAP35x TRM
  375. * it mentions reducing DMA transfer length by one element in master
  376. * normal mode.
  377. */
  378. if (mcspi->fifo_depth == 0)
  379. transfer_reduction = es;
  380. word_len = cs->word_len;
  381. l = mcspi_cached_chconf0(spi);
  382. if (word_len <= 8)
  383. element_count = count;
  384. else if (word_len <= 16)
  385. element_count = count >> 1;
  386. else /* word_len <= 32 */
  387. element_count = count >> 2;
  388. dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
  389. /*
  390. * Reduce DMA transfer length by one more if McSPI is
  391. * configured in turbo mode.
  392. */
  393. if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
  394. transfer_reduction += es;
  395. if (transfer_reduction) {
  396. /* Split sgl into two. The second sgl won't be used. */
  397. sizes[0] = count - transfer_reduction;
  398. sizes[1] = transfer_reduction;
  399. nb_sizes = 2;
  400. } else {
  401. /*
  402. * Don't bother splitting the sgl. This essentially
  403. * clones the original sgl.
  404. */
  405. sizes[0] = count;
  406. nb_sizes = 1;
  407. }
  408. ret = sg_split(xfer->rx_sg.sgl, xfer->rx_sg.nents, 0, nb_sizes,
  409. sizes, sg_out, out_mapped_nents, GFP_KERNEL);
  410. if (ret < 0) {
  411. dev_err(&spi->dev, "sg_split failed\n");
  412. return 0;
  413. }
  414. tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, sg_out[0],
  415. out_mapped_nents[0], DMA_DEV_TO_MEM,
  416. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  417. if (tx) {
  418. tx->callback = omap2_mcspi_rx_callback;
  419. tx->callback_param = spi;
  420. dmaengine_submit(tx);
  421. } else {
  422. /* FIXME: fall back to PIO? */
  423. }
  424. dma_async_issue_pending(mcspi_dma->dma_rx);
  425. omap2_mcspi_set_dma_req(spi, 1, 1);
  426. ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_rx_completion);
  427. if (ret || mcspi->slave_aborted) {
  428. dmaengine_terminate_sync(mcspi_dma->dma_rx);
  429. omap2_mcspi_set_dma_req(spi, 1, 0);
  430. return 0;
  431. }
  432. for (x = 0; x < nb_sizes; x++)
  433. kfree(sg_out[x]);
  434. if (mcspi->fifo_depth > 0)
  435. return count;
  436. /*
  437. * Due to the DMA transfer length reduction the missing bytes must
  438. * be read manually to receive all of the expected data.
  439. */
  440. omap2_mcspi_set_enable(spi, 0);
  441. elements = element_count - 1;
  442. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  443. elements--;
  444. if (!mcspi_wait_for_reg_bit(chstat_reg,
  445. OMAP2_MCSPI_CHSTAT_RXS)) {
  446. u32 w;
  447. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  448. if (word_len <= 8)
  449. ((u8 *)xfer->rx_buf)[elements++] = w;
  450. else if (word_len <= 16)
  451. ((u16 *)xfer->rx_buf)[elements++] = w;
  452. else /* word_len <= 32 */
  453. ((u32 *)xfer->rx_buf)[elements++] = w;
  454. } else {
  455. int bytes_per_word = mcspi_bytes_per_word(word_len);
  456. dev_err(&spi->dev, "DMA RX penultimate word empty\n");
  457. count -= (bytes_per_word << 1);
  458. omap2_mcspi_set_enable(spi, 1);
  459. return count;
  460. }
  461. }
  462. if (!mcspi_wait_for_reg_bit(chstat_reg, OMAP2_MCSPI_CHSTAT_RXS)) {
  463. u32 w;
  464. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  465. if (word_len <= 8)
  466. ((u8 *)xfer->rx_buf)[elements] = w;
  467. else if (word_len <= 16)
  468. ((u16 *)xfer->rx_buf)[elements] = w;
  469. else /* word_len <= 32 */
  470. ((u32 *)xfer->rx_buf)[elements] = w;
  471. } else {
  472. dev_err(&spi->dev, "DMA RX last word empty\n");
  473. count -= mcspi_bytes_per_word(word_len);
  474. }
  475. omap2_mcspi_set_enable(spi, 1);
  476. return count;
  477. }
  478. static unsigned
  479. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  480. {
  481. struct omap2_mcspi *mcspi;
  482. struct omap2_mcspi_cs *cs = spi->controller_state;
  483. struct omap2_mcspi_dma *mcspi_dma;
  484. unsigned int count;
  485. u8 *rx;
  486. const u8 *tx;
  487. struct dma_slave_config cfg;
  488. enum dma_slave_buswidth width;
  489. unsigned es;
  490. void __iomem *chstat_reg;
  491. void __iomem *irqstat_reg;
  492. int wait_res;
  493. mcspi = spi_master_get_devdata(spi->master);
  494. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  495. if (cs->word_len <= 8) {
  496. width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  497. es = 1;
  498. } else if (cs->word_len <= 16) {
  499. width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  500. es = 2;
  501. } else {
  502. width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  503. es = 4;
  504. }
  505. count = xfer->len;
  506. memset(&cfg, 0, sizeof(cfg));
  507. cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
  508. cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
  509. cfg.src_addr_width = width;
  510. cfg.dst_addr_width = width;
  511. cfg.src_maxburst = 1;
  512. cfg.dst_maxburst = 1;
  513. rx = xfer->rx_buf;
  514. tx = xfer->tx_buf;
  515. mcspi->slave_aborted = false;
  516. reinit_completion(&mcspi_dma->dma_tx_completion);
  517. reinit_completion(&mcspi_dma->dma_rx_completion);
  518. reinit_completion(&mcspi->txdone);
  519. if (tx) {
  520. /* Enable EOW IRQ to know end of tx in slave mode */
  521. if (spi_controller_is_slave(spi->master))
  522. mcspi_write_reg(spi->master,
  523. OMAP2_MCSPI_IRQENABLE,
  524. OMAP2_MCSPI_IRQSTATUS_EOW);
  525. omap2_mcspi_tx_dma(spi, xfer, cfg);
  526. }
  527. if (rx != NULL)
  528. count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
  529. if (tx != NULL) {
  530. int ret;
  531. ret = mcspi_wait_for_completion(mcspi, &mcspi_dma->dma_tx_completion);
  532. if (ret || mcspi->slave_aborted) {
  533. dmaengine_terminate_sync(mcspi_dma->dma_tx);
  534. omap2_mcspi_set_dma_req(spi, 0, 0);
  535. return 0;
  536. }
  537. if (spi_controller_is_slave(mcspi->master)) {
  538. ret = mcspi_wait_for_completion(mcspi, &mcspi->txdone);
  539. if (ret || mcspi->slave_aborted)
  540. return 0;
  541. }
  542. if (mcspi->fifo_depth > 0) {
  543. irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
  544. if (mcspi_wait_for_reg_bit(irqstat_reg,
  545. OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
  546. dev_err(&spi->dev, "EOW timed out\n");
  547. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
  548. OMAP2_MCSPI_IRQSTATUS_EOW);
  549. }
  550. /* for TX_ONLY mode, be sure all words have shifted out */
  551. if (rx == NULL) {
  552. chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
  553. if (mcspi->fifo_depth > 0) {
  554. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  555. OMAP2_MCSPI_CHSTAT_TXFFE);
  556. if (wait_res < 0)
  557. dev_err(&spi->dev, "TXFFE timed out\n");
  558. } else {
  559. wait_res = mcspi_wait_for_reg_bit(chstat_reg,
  560. OMAP2_MCSPI_CHSTAT_TXS);
  561. if (wait_res < 0)
  562. dev_err(&spi->dev, "TXS timed out\n");
  563. }
  564. if (wait_res >= 0 &&
  565. (mcspi_wait_for_reg_bit(chstat_reg,
  566. OMAP2_MCSPI_CHSTAT_EOT) < 0))
  567. dev_err(&spi->dev, "EOT timed out\n");
  568. }
  569. }
  570. return count;
  571. }
  572. static unsigned
  573. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  574. {
  575. struct omap2_mcspi_cs *cs = spi->controller_state;
  576. unsigned int count, c;
  577. u32 l;
  578. void __iomem *base = cs->base;
  579. void __iomem *tx_reg;
  580. void __iomem *rx_reg;
  581. void __iomem *chstat_reg;
  582. int word_len;
  583. count = xfer->len;
  584. c = count;
  585. word_len = cs->word_len;
  586. l = mcspi_cached_chconf0(spi);
  587. /* We store the pre-calculated register addresses on stack to speed
  588. * up the transfer loop. */
  589. tx_reg = base + OMAP2_MCSPI_TX0;
  590. rx_reg = base + OMAP2_MCSPI_RX0;
  591. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  592. if (c < (word_len>>3))
  593. return 0;
  594. if (word_len <= 8) {
  595. u8 *rx;
  596. const u8 *tx;
  597. rx = xfer->rx_buf;
  598. tx = xfer->tx_buf;
  599. do {
  600. c -= 1;
  601. if (tx != NULL) {
  602. if (mcspi_wait_for_reg_bit(chstat_reg,
  603. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  604. dev_err(&spi->dev, "TXS timed out\n");
  605. goto out;
  606. }
  607. dev_vdbg(&spi->dev, "write-%d %02x\n",
  608. word_len, *tx);
  609. writel_relaxed(*tx++, tx_reg);
  610. }
  611. if (rx != NULL) {
  612. if (mcspi_wait_for_reg_bit(chstat_reg,
  613. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  614. dev_err(&spi->dev, "RXS timed out\n");
  615. goto out;
  616. }
  617. if (c == 1 && tx == NULL &&
  618. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  619. omap2_mcspi_set_enable(spi, 0);
  620. *rx++ = readl_relaxed(rx_reg);
  621. dev_vdbg(&spi->dev, "read-%d %02x\n",
  622. word_len, *(rx - 1));
  623. if (mcspi_wait_for_reg_bit(chstat_reg,
  624. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  625. dev_err(&spi->dev,
  626. "RXS timed out\n");
  627. goto out;
  628. }
  629. c = 0;
  630. } else if (c == 0 && tx == NULL) {
  631. omap2_mcspi_set_enable(spi, 0);
  632. }
  633. *rx++ = readl_relaxed(rx_reg);
  634. dev_vdbg(&spi->dev, "read-%d %02x\n",
  635. word_len, *(rx - 1));
  636. }
  637. /* Add word delay between each word */
  638. spi_delay_exec(&xfer->word_delay, xfer);
  639. } while (c);
  640. } else if (word_len <= 16) {
  641. u16 *rx;
  642. const u16 *tx;
  643. rx = xfer->rx_buf;
  644. tx = xfer->tx_buf;
  645. do {
  646. c -= 2;
  647. if (tx != NULL) {
  648. if (mcspi_wait_for_reg_bit(chstat_reg,
  649. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  650. dev_err(&spi->dev, "TXS timed out\n");
  651. goto out;
  652. }
  653. dev_vdbg(&spi->dev, "write-%d %04x\n",
  654. word_len, *tx);
  655. writel_relaxed(*tx++, tx_reg);
  656. }
  657. if (rx != NULL) {
  658. if (mcspi_wait_for_reg_bit(chstat_reg,
  659. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  660. dev_err(&spi->dev, "RXS timed out\n");
  661. goto out;
  662. }
  663. if (c == 2 && tx == NULL &&
  664. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  665. omap2_mcspi_set_enable(spi, 0);
  666. *rx++ = readl_relaxed(rx_reg);
  667. dev_vdbg(&spi->dev, "read-%d %04x\n",
  668. word_len, *(rx - 1));
  669. if (mcspi_wait_for_reg_bit(chstat_reg,
  670. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  671. dev_err(&spi->dev,
  672. "RXS timed out\n");
  673. goto out;
  674. }
  675. c = 0;
  676. } else if (c == 0 && tx == NULL) {
  677. omap2_mcspi_set_enable(spi, 0);
  678. }
  679. *rx++ = readl_relaxed(rx_reg);
  680. dev_vdbg(&spi->dev, "read-%d %04x\n",
  681. word_len, *(rx - 1));
  682. }
  683. /* Add word delay between each word */
  684. spi_delay_exec(&xfer->word_delay, xfer);
  685. } while (c >= 2);
  686. } else if (word_len <= 32) {
  687. u32 *rx;
  688. const u32 *tx;
  689. rx = xfer->rx_buf;
  690. tx = xfer->tx_buf;
  691. do {
  692. c -= 4;
  693. if (tx != NULL) {
  694. if (mcspi_wait_for_reg_bit(chstat_reg,
  695. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  696. dev_err(&spi->dev, "TXS timed out\n");
  697. goto out;
  698. }
  699. dev_vdbg(&spi->dev, "write-%d %08x\n",
  700. word_len, *tx);
  701. writel_relaxed(*tx++, tx_reg);
  702. }
  703. if (rx != NULL) {
  704. if (mcspi_wait_for_reg_bit(chstat_reg,
  705. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  706. dev_err(&spi->dev, "RXS timed out\n");
  707. goto out;
  708. }
  709. if (c == 4 && tx == NULL &&
  710. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  711. omap2_mcspi_set_enable(spi, 0);
  712. *rx++ = readl_relaxed(rx_reg);
  713. dev_vdbg(&spi->dev, "read-%d %08x\n",
  714. word_len, *(rx - 1));
  715. if (mcspi_wait_for_reg_bit(chstat_reg,
  716. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  717. dev_err(&spi->dev,
  718. "RXS timed out\n");
  719. goto out;
  720. }
  721. c = 0;
  722. } else if (c == 0 && tx == NULL) {
  723. omap2_mcspi_set_enable(spi, 0);
  724. }
  725. *rx++ = readl_relaxed(rx_reg);
  726. dev_vdbg(&spi->dev, "read-%d %08x\n",
  727. word_len, *(rx - 1));
  728. }
  729. /* Add word delay between each word */
  730. spi_delay_exec(&xfer->word_delay, xfer);
  731. } while (c >= 4);
  732. }
  733. /* for TX_ONLY mode, be sure all words have shifted out */
  734. if (xfer->rx_buf == NULL) {
  735. if (mcspi_wait_for_reg_bit(chstat_reg,
  736. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  737. dev_err(&spi->dev, "TXS timed out\n");
  738. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  739. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  740. dev_err(&spi->dev, "EOT timed out\n");
  741. /* disable chan to purge rx datas received in TX_ONLY transfer,
  742. * otherwise these rx datas will affect the direct following
  743. * RX_ONLY transfer.
  744. */
  745. omap2_mcspi_set_enable(spi, 0);
  746. }
  747. out:
  748. omap2_mcspi_set_enable(spi, 1);
  749. return count - c;
  750. }
  751. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  752. {
  753. u32 div;
  754. for (div = 0; div < 15; div++)
  755. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  756. return div;
  757. return 15;
  758. }
  759. /* called only when no transfer is active to this device */
  760. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  761. struct spi_transfer *t)
  762. {
  763. struct omap2_mcspi_cs *cs = spi->controller_state;
  764. struct omap2_mcspi *mcspi;
  765. u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
  766. u8 word_len = spi->bits_per_word;
  767. u32 speed_hz = spi->max_speed_hz;
  768. mcspi = spi_master_get_devdata(spi->master);
  769. if (t != NULL && t->bits_per_word)
  770. word_len = t->bits_per_word;
  771. cs->word_len = word_len;
  772. if (t && t->speed_hz)
  773. speed_hz = t->speed_hz;
  774. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  775. if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
  776. clkd = omap2_mcspi_calc_divisor(speed_hz);
  777. speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
  778. clkg = 0;
  779. } else {
  780. div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
  781. speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
  782. clkd = (div - 1) & 0xf;
  783. extclk = (div - 1) >> 4;
  784. clkg = OMAP2_MCSPI_CHCONF_CLKG;
  785. }
  786. l = mcspi_cached_chconf0(spi);
  787. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  788. * REVISIT: this controller could support SPI_3WIRE mode.
  789. */
  790. if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  791. l &= ~OMAP2_MCSPI_CHCONF_IS;
  792. l &= ~OMAP2_MCSPI_CHCONF_DPE1;
  793. l |= OMAP2_MCSPI_CHCONF_DPE0;
  794. } else {
  795. l |= OMAP2_MCSPI_CHCONF_IS;
  796. l |= OMAP2_MCSPI_CHCONF_DPE1;
  797. l &= ~OMAP2_MCSPI_CHCONF_DPE0;
  798. }
  799. /* wordlength */
  800. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  801. l |= (word_len - 1) << 7;
  802. /* set chipselect polarity; manage with FORCE */
  803. if (!(spi->mode & SPI_CS_HIGH))
  804. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  805. else
  806. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  807. /* set clock divisor */
  808. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  809. l |= clkd << 2;
  810. /* set clock granularity */
  811. l &= ~OMAP2_MCSPI_CHCONF_CLKG;
  812. l |= clkg;
  813. if (clkg) {
  814. cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
  815. cs->chctrl0 |= extclk << 8;
  816. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
  817. }
  818. /* set SPI mode 0..3 */
  819. if (spi->mode & SPI_CPOL)
  820. l |= OMAP2_MCSPI_CHCONF_POL;
  821. else
  822. l &= ~OMAP2_MCSPI_CHCONF_POL;
  823. if (spi->mode & SPI_CPHA)
  824. l |= OMAP2_MCSPI_CHCONF_PHA;
  825. else
  826. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  827. mcspi_write_chconf0(spi, l);
  828. cs->mode = spi->mode;
  829. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  830. speed_hz,
  831. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  832. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  833. return 0;
  834. }
  835. /*
  836. * Note that we currently allow DMA only if we get a channel
  837. * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
  838. */
  839. static int omap2_mcspi_request_dma(struct omap2_mcspi *mcspi,
  840. struct omap2_mcspi_dma *mcspi_dma)
  841. {
  842. int ret = 0;
  843. mcspi_dma->dma_rx = dma_request_chan(mcspi->dev,
  844. mcspi_dma->dma_rx_ch_name);
  845. if (IS_ERR(mcspi_dma->dma_rx)) {
  846. ret = PTR_ERR(mcspi_dma->dma_rx);
  847. mcspi_dma->dma_rx = NULL;
  848. goto no_dma;
  849. }
  850. mcspi_dma->dma_tx = dma_request_chan(mcspi->dev,
  851. mcspi_dma->dma_tx_ch_name);
  852. if (IS_ERR(mcspi_dma->dma_tx)) {
  853. ret = PTR_ERR(mcspi_dma->dma_tx);
  854. mcspi_dma->dma_tx = NULL;
  855. dma_release_channel(mcspi_dma->dma_rx);
  856. mcspi_dma->dma_rx = NULL;
  857. }
  858. init_completion(&mcspi_dma->dma_rx_completion);
  859. init_completion(&mcspi_dma->dma_tx_completion);
  860. no_dma:
  861. return ret;
  862. }
  863. static void omap2_mcspi_release_dma(struct spi_master *master)
  864. {
  865. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  866. struct omap2_mcspi_dma *mcspi_dma;
  867. int i;
  868. for (i = 0; i < master->num_chipselect; i++) {
  869. mcspi_dma = &mcspi->dma_channels[i];
  870. if (mcspi_dma->dma_rx) {
  871. dma_release_channel(mcspi_dma->dma_rx);
  872. mcspi_dma->dma_rx = NULL;
  873. }
  874. if (mcspi_dma->dma_tx) {
  875. dma_release_channel(mcspi_dma->dma_tx);
  876. mcspi_dma->dma_tx = NULL;
  877. }
  878. }
  879. }
  880. static void omap2_mcspi_cleanup(struct spi_device *spi)
  881. {
  882. struct omap2_mcspi_cs *cs;
  883. if (spi->controller_state) {
  884. /* Unlink controller state from context save list */
  885. cs = spi->controller_state;
  886. list_del(&cs->node);
  887. kfree(cs);
  888. }
  889. }
  890. static int omap2_mcspi_setup(struct spi_device *spi)
  891. {
  892. bool initial_setup = false;
  893. int ret;
  894. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  895. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  896. struct omap2_mcspi_cs *cs = spi->controller_state;
  897. if (!cs) {
  898. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  899. if (!cs)
  900. return -ENOMEM;
  901. cs->base = mcspi->base + spi->chip_select * 0x14;
  902. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  903. cs->mode = 0;
  904. cs->chconf0 = 0;
  905. cs->chctrl0 = 0;
  906. spi->controller_state = cs;
  907. /* Link this to context save list */
  908. list_add_tail(&cs->node, &ctx->cs);
  909. initial_setup = true;
  910. }
  911. ret = pm_runtime_resume_and_get(mcspi->dev);
  912. if (ret < 0) {
  913. if (initial_setup)
  914. omap2_mcspi_cleanup(spi);
  915. return ret;
  916. }
  917. ret = omap2_mcspi_setup_transfer(spi, NULL);
  918. if (ret && initial_setup)
  919. omap2_mcspi_cleanup(spi);
  920. pm_runtime_mark_last_busy(mcspi->dev);
  921. pm_runtime_put_autosuspend(mcspi->dev);
  922. return ret;
  923. }
  924. static irqreturn_t omap2_mcspi_irq_handler(int irq, void *data)
  925. {
  926. struct omap2_mcspi *mcspi = data;
  927. u32 irqstat;
  928. irqstat = mcspi_read_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS);
  929. if (!irqstat)
  930. return IRQ_NONE;
  931. /* Disable IRQ and wakeup slave xfer task */
  932. mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQENABLE, 0);
  933. if (irqstat & OMAP2_MCSPI_IRQSTATUS_EOW)
  934. complete(&mcspi->txdone);
  935. return IRQ_HANDLED;
  936. }
  937. static int omap2_mcspi_slave_abort(struct spi_master *master)
  938. {
  939. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  940. struct omap2_mcspi_dma *mcspi_dma = mcspi->dma_channels;
  941. mcspi->slave_aborted = true;
  942. complete(&mcspi_dma->dma_rx_completion);
  943. complete(&mcspi_dma->dma_tx_completion);
  944. complete(&mcspi->txdone);
  945. return 0;
  946. }
  947. static int omap2_mcspi_transfer_one(struct spi_master *master,
  948. struct spi_device *spi,
  949. struct spi_transfer *t)
  950. {
  951. /* We only enable one channel at a time -- the one whose message is
  952. * -- although this controller would gladly
  953. * arbitrate among multiple channels. This corresponds to "single
  954. * channel" master mode. As a side effect, we need to manage the
  955. * chipselect with the FORCE bit ... CS != channel enable.
  956. */
  957. struct omap2_mcspi *mcspi;
  958. struct omap2_mcspi_dma *mcspi_dma;
  959. struct omap2_mcspi_cs *cs;
  960. struct omap2_mcspi_device_config *cd;
  961. int par_override = 0;
  962. int status = 0;
  963. u32 chconf;
  964. mcspi = spi_master_get_devdata(master);
  965. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  966. cs = spi->controller_state;
  967. cd = spi->controller_data;
  968. /*
  969. * The slave driver could have changed spi->mode in which case
  970. * it will be different from cs->mode (the current hardware setup).
  971. * If so, set par_override (even though its not a parity issue) so
  972. * omap2_mcspi_setup_transfer will be called to configure the hardware
  973. * with the correct mode on the first iteration of the loop below.
  974. */
  975. if (spi->mode != cs->mode)
  976. par_override = 1;
  977. omap2_mcspi_set_enable(spi, 0);
  978. if (spi->cs_gpiod)
  979. omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
  980. if (par_override ||
  981. (t->speed_hz != spi->max_speed_hz) ||
  982. (t->bits_per_word != spi->bits_per_word)) {
  983. par_override = 1;
  984. status = omap2_mcspi_setup_transfer(spi, t);
  985. if (status < 0)
  986. goto out;
  987. if (t->speed_hz == spi->max_speed_hz &&
  988. t->bits_per_word == spi->bits_per_word)
  989. par_override = 0;
  990. }
  991. if (cd && cd->cs_per_word) {
  992. chconf = mcspi->ctx.modulctrl;
  993. chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
  994. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  995. mcspi->ctx.modulctrl =
  996. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  997. }
  998. chconf = mcspi_cached_chconf0(spi);
  999. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  1000. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  1001. if (t->tx_buf == NULL)
  1002. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  1003. else if (t->rx_buf == NULL)
  1004. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  1005. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  1006. /* Turbo mode is for more than one word */
  1007. if (t->len > ((cs->word_len + 7) >> 3))
  1008. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  1009. }
  1010. mcspi_write_chconf0(spi, chconf);
  1011. if (t->len) {
  1012. unsigned count;
  1013. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  1014. master->cur_msg_mapped &&
  1015. master->can_dma(master, spi, t))
  1016. omap2_mcspi_set_fifo(spi, t, 1);
  1017. omap2_mcspi_set_enable(spi, 1);
  1018. /* RX_ONLY mode needs dummy data in TX reg */
  1019. if (t->tx_buf == NULL)
  1020. writel_relaxed(0, cs->base
  1021. + OMAP2_MCSPI_TX0);
  1022. if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
  1023. master->cur_msg_mapped &&
  1024. master->can_dma(master, spi, t))
  1025. count = omap2_mcspi_txrx_dma(spi, t);
  1026. else
  1027. count = omap2_mcspi_txrx_pio(spi, t);
  1028. if (count != t->len) {
  1029. status = -EIO;
  1030. goto out;
  1031. }
  1032. }
  1033. omap2_mcspi_set_enable(spi, 0);
  1034. if (mcspi->fifo_depth > 0)
  1035. omap2_mcspi_set_fifo(spi, t, 0);
  1036. out:
  1037. /* Restore defaults if they were overriden */
  1038. if (par_override) {
  1039. par_override = 0;
  1040. status = omap2_mcspi_setup_transfer(spi, NULL);
  1041. }
  1042. if (cd && cd->cs_per_word) {
  1043. chconf = mcspi->ctx.modulctrl;
  1044. chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
  1045. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
  1046. mcspi->ctx.modulctrl =
  1047. mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
  1048. }
  1049. omap2_mcspi_set_enable(spi, 0);
  1050. if (spi->cs_gpiod)
  1051. omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
  1052. if (mcspi->fifo_depth > 0 && t)
  1053. omap2_mcspi_set_fifo(spi, t, 0);
  1054. return status;
  1055. }
  1056. static int omap2_mcspi_prepare_message(struct spi_master *master,
  1057. struct spi_message *msg)
  1058. {
  1059. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1060. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1061. struct omap2_mcspi_cs *cs;
  1062. /* Only a single channel can have the FORCE bit enabled
  1063. * in its chconf0 register.
  1064. * Scan all channels and disable them except the current one.
  1065. * A FORCE can remain from a last transfer having cs_change enabled
  1066. */
  1067. list_for_each_entry(cs, &ctx->cs, node) {
  1068. if (msg->spi->controller_state == cs)
  1069. continue;
  1070. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE)) {
  1071. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1072. writel_relaxed(cs->chconf0,
  1073. cs->base + OMAP2_MCSPI_CHCONF0);
  1074. readl_relaxed(cs->base + OMAP2_MCSPI_CHCONF0);
  1075. }
  1076. }
  1077. return 0;
  1078. }
  1079. static bool omap2_mcspi_can_dma(struct spi_master *master,
  1080. struct spi_device *spi,
  1081. struct spi_transfer *xfer)
  1082. {
  1083. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  1084. struct omap2_mcspi_dma *mcspi_dma =
  1085. &mcspi->dma_channels[spi->chip_select];
  1086. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx)
  1087. return false;
  1088. if (spi_controller_is_slave(master))
  1089. return true;
  1090. master->dma_rx = mcspi_dma->dma_rx;
  1091. master->dma_tx = mcspi_dma->dma_tx;
  1092. return (xfer->len >= DMA_MIN_BYTES);
  1093. }
  1094. static size_t omap2_mcspi_max_xfer_size(struct spi_device *spi)
  1095. {
  1096. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  1097. struct omap2_mcspi_dma *mcspi_dma =
  1098. &mcspi->dma_channels[spi->chip_select];
  1099. if (mcspi->max_xfer_len && mcspi_dma->dma_rx)
  1100. return mcspi->max_xfer_len;
  1101. return SIZE_MAX;
  1102. }
  1103. static int omap2_mcspi_controller_setup(struct omap2_mcspi *mcspi)
  1104. {
  1105. struct spi_master *master = mcspi->master;
  1106. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1107. int ret = 0;
  1108. ret = pm_runtime_resume_and_get(mcspi->dev);
  1109. if (ret < 0)
  1110. return ret;
  1111. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  1112. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  1113. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  1114. omap2_mcspi_set_mode(master);
  1115. pm_runtime_mark_last_busy(mcspi->dev);
  1116. pm_runtime_put_autosuspend(mcspi->dev);
  1117. return 0;
  1118. }
  1119. static int omap_mcspi_runtime_suspend(struct device *dev)
  1120. {
  1121. int error;
  1122. error = pinctrl_pm_select_idle_state(dev);
  1123. if (error)
  1124. dev_warn(dev, "%s: failed to set pins: %i\n", __func__, error);
  1125. return 0;
  1126. }
  1127. /*
  1128. * When SPI wake up from off-mode, CS is in activate state. If it was in
  1129. * inactive state when driver was suspend, then force it to inactive state at
  1130. * wake up.
  1131. */
  1132. static int omap_mcspi_runtime_resume(struct device *dev)
  1133. {
  1134. struct spi_master *master = dev_get_drvdata(dev);
  1135. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1136. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  1137. struct omap2_mcspi_cs *cs;
  1138. int error;
  1139. error = pinctrl_pm_select_default_state(dev);
  1140. if (error)
  1141. dev_warn(dev, "%s: failed to set pins: %i\n", __func__, error);
  1142. /* McSPI: context restore */
  1143. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
  1144. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
  1145. list_for_each_entry(cs, &ctx->cs, node) {
  1146. /*
  1147. * We need to toggle CS state for OMAP take this
  1148. * change in account.
  1149. */
  1150. if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
  1151. cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
  1152. writel_relaxed(cs->chconf0,
  1153. cs->base + OMAP2_MCSPI_CHCONF0);
  1154. cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
  1155. writel_relaxed(cs->chconf0,
  1156. cs->base + OMAP2_MCSPI_CHCONF0);
  1157. } else {
  1158. writel_relaxed(cs->chconf0,
  1159. cs->base + OMAP2_MCSPI_CHCONF0);
  1160. }
  1161. }
  1162. return 0;
  1163. }
  1164. static struct omap2_mcspi_platform_config omap2_pdata = {
  1165. .regs_offset = 0,
  1166. };
  1167. static struct omap2_mcspi_platform_config omap4_pdata = {
  1168. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  1169. };
  1170. static struct omap2_mcspi_platform_config am654_pdata = {
  1171. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  1172. .max_xfer_len = SZ_4K - 1,
  1173. };
  1174. static const struct of_device_id omap_mcspi_of_match[] = {
  1175. {
  1176. .compatible = "ti,omap2-mcspi",
  1177. .data = &omap2_pdata,
  1178. },
  1179. {
  1180. .compatible = "ti,omap4-mcspi",
  1181. .data = &omap4_pdata,
  1182. },
  1183. {
  1184. .compatible = "ti,am654-mcspi",
  1185. .data = &am654_pdata,
  1186. },
  1187. { },
  1188. };
  1189. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  1190. static int omap2_mcspi_probe(struct platform_device *pdev)
  1191. {
  1192. struct spi_master *master;
  1193. const struct omap2_mcspi_platform_config *pdata;
  1194. struct omap2_mcspi *mcspi;
  1195. struct resource *r;
  1196. int status = 0, i;
  1197. u32 regs_offset = 0;
  1198. struct device_node *node = pdev->dev.of_node;
  1199. const struct of_device_id *match;
  1200. if (of_property_read_bool(node, "spi-slave"))
  1201. master = spi_alloc_slave(&pdev->dev, sizeof(*mcspi));
  1202. else
  1203. master = spi_alloc_master(&pdev->dev, sizeof(*mcspi));
  1204. if (!master)
  1205. return -ENOMEM;
  1206. /* the spi->mode bits understood by this driver: */
  1207. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1208. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1209. master->setup = omap2_mcspi_setup;
  1210. master->auto_runtime_pm = true;
  1211. master->prepare_message = omap2_mcspi_prepare_message;
  1212. master->can_dma = omap2_mcspi_can_dma;
  1213. master->transfer_one = omap2_mcspi_transfer_one;
  1214. master->set_cs = omap2_mcspi_set_cs;
  1215. master->cleanup = omap2_mcspi_cleanup;
  1216. master->slave_abort = omap2_mcspi_slave_abort;
  1217. master->dev.of_node = node;
  1218. master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
  1219. master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
  1220. master->use_gpio_descriptors = true;
  1221. platform_set_drvdata(pdev, master);
  1222. mcspi = spi_master_get_devdata(master);
  1223. mcspi->master = master;
  1224. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  1225. if (match) {
  1226. u32 num_cs = 1; /* default number of chipselect */
  1227. pdata = match->data;
  1228. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  1229. master->num_chipselect = num_cs;
  1230. if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
  1231. mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  1232. } else {
  1233. pdata = dev_get_platdata(&pdev->dev);
  1234. master->num_chipselect = pdata->num_cs;
  1235. mcspi->pin_dir = pdata->pin_dir;
  1236. }
  1237. regs_offset = pdata->regs_offset;
  1238. if (pdata->max_xfer_len) {
  1239. mcspi->max_xfer_len = pdata->max_xfer_len;
  1240. master->max_transfer_size = omap2_mcspi_max_xfer_size;
  1241. }
  1242. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1243. mcspi->base = devm_ioremap_resource(&pdev->dev, r);
  1244. if (IS_ERR(mcspi->base)) {
  1245. status = PTR_ERR(mcspi->base);
  1246. goto free_master;
  1247. }
  1248. mcspi->phys = r->start + regs_offset;
  1249. mcspi->base += regs_offset;
  1250. mcspi->dev = &pdev->dev;
  1251. INIT_LIST_HEAD(&mcspi->ctx.cs);
  1252. mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
  1253. sizeof(struct omap2_mcspi_dma),
  1254. GFP_KERNEL);
  1255. if (mcspi->dma_channels == NULL) {
  1256. status = -ENOMEM;
  1257. goto free_master;
  1258. }
  1259. for (i = 0; i < master->num_chipselect; i++) {
  1260. sprintf(mcspi->dma_channels[i].dma_rx_ch_name, "rx%d", i);
  1261. sprintf(mcspi->dma_channels[i].dma_tx_ch_name, "tx%d", i);
  1262. status = omap2_mcspi_request_dma(mcspi,
  1263. &mcspi->dma_channels[i]);
  1264. if (status == -EPROBE_DEFER)
  1265. goto free_master;
  1266. }
  1267. status = platform_get_irq(pdev, 0);
  1268. if (status < 0) {
  1269. dev_err_probe(&pdev->dev, status, "no irq resource found\n");
  1270. goto free_master;
  1271. }
  1272. init_completion(&mcspi->txdone);
  1273. status = devm_request_irq(&pdev->dev, status,
  1274. omap2_mcspi_irq_handler, 0, pdev->name,
  1275. mcspi);
  1276. if (status) {
  1277. dev_err(&pdev->dev, "Cannot request IRQ");
  1278. goto free_master;
  1279. }
  1280. pm_runtime_use_autosuspend(&pdev->dev);
  1281. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1282. pm_runtime_enable(&pdev->dev);
  1283. status = omap2_mcspi_controller_setup(mcspi);
  1284. if (status < 0)
  1285. goto disable_pm;
  1286. status = devm_spi_register_controller(&pdev->dev, master);
  1287. if (status < 0)
  1288. goto disable_pm;
  1289. return status;
  1290. disable_pm:
  1291. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1292. pm_runtime_put_sync(&pdev->dev);
  1293. pm_runtime_disable(&pdev->dev);
  1294. free_master:
  1295. omap2_mcspi_release_dma(master);
  1296. spi_master_put(master);
  1297. return status;
  1298. }
  1299. static int omap2_mcspi_remove(struct platform_device *pdev)
  1300. {
  1301. struct spi_master *master = platform_get_drvdata(pdev);
  1302. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1303. omap2_mcspi_release_dma(master);
  1304. pm_runtime_dont_use_autosuspend(mcspi->dev);
  1305. pm_runtime_put_sync(mcspi->dev);
  1306. pm_runtime_disable(&pdev->dev);
  1307. return 0;
  1308. }
  1309. /* work with hotplug and coldplug */
  1310. MODULE_ALIAS("platform:omap2_mcspi");
  1311. static int __maybe_unused omap2_mcspi_suspend(struct device *dev)
  1312. {
  1313. struct spi_master *master = dev_get_drvdata(dev);
  1314. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1315. int error;
  1316. error = pinctrl_pm_select_sleep_state(dev);
  1317. if (error)
  1318. dev_warn(mcspi->dev, "%s: failed to set pins: %i\n",
  1319. __func__, error);
  1320. error = spi_master_suspend(master);
  1321. if (error)
  1322. dev_warn(mcspi->dev, "%s: master suspend failed: %i\n",
  1323. __func__, error);
  1324. return pm_runtime_force_suspend(dev);
  1325. }
  1326. static int __maybe_unused omap2_mcspi_resume(struct device *dev)
  1327. {
  1328. struct spi_master *master = dev_get_drvdata(dev);
  1329. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  1330. int error;
  1331. error = spi_master_resume(master);
  1332. if (error)
  1333. dev_warn(mcspi->dev, "%s: master resume failed: %i\n",
  1334. __func__, error);
  1335. return pm_runtime_force_resume(dev);
  1336. }
  1337. static const struct dev_pm_ops omap2_mcspi_pm_ops = {
  1338. SET_SYSTEM_SLEEP_PM_OPS(omap2_mcspi_suspend,
  1339. omap2_mcspi_resume)
  1340. .runtime_suspend = omap_mcspi_runtime_suspend,
  1341. .runtime_resume = omap_mcspi_runtime_resume,
  1342. };
  1343. static struct platform_driver omap2_mcspi_driver = {
  1344. .driver = {
  1345. .name = "omap2_mcspi",
  1346. .pm = &omap2_mcspi_pm_ops,
  1347. .of_match_table = omap_mcspi_of_match,
  1348. },
  1349. .probe = omap2_mcspi_probe,
  1350. .remove = omap2_mcspi_remove,
  1351. };
  1352. module_platform_driver(omap2_mcspi_driver);
  1353. MODULE_LICENSE("GPL");