spi-omap2-mcspi.c 38 KB

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