spi-omap2-mcspi.c 32 KB

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