spi-omap2-mcspi.c 39 KB

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