spi-omap2-mcspi.c 33 KB

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