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