spi-omap2-mcspi.c 34 KB

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