spi-omap2-mcspi.c 33 KB

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