spi-omap2-mcspi.c 39 KB

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