spi-omap2-mcspi.c 41 KB

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