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(mcspi->dev, xfer->tx_dma, count,
  363. DMA_TO_DEVICE);
  364. /* for TX_ONLY mode, be sure all words have shifted out */
  365. if (rx == NULL) {
  366. if (mcspi_wait_for_reg_bit(chstat_reg,
  367. OMAP2_MCSPI_CHSTAT_TXS) < 0)
  368. dev_err(&spi->dev, "TXS timed out\n");
  369. else if (mcspi_wait_for_reg_bit(chstat_reg,
  370. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  371. dev_err(&spi->dev, "EOT timed out\n");
  372. }
  373. }
  374. if (rx != NULL) {
  375. wait_for_completion(&mcspi_dma->dma_rx_completion);
  376. dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
  377. DMA_FROM_DEVICE);
  378. omap2_mcspi_set_enable(spi, 0);
  379. elements = element_count - 1;
  380. if (l & OMAP2_MCSPI_CHCONF_TURBO) {
  381. elements--;
  382. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  383. & OMAP2_MCSPI_CHSTAT_RXS)) {
  384. u32 w;
  385. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  386. if (word_len <= 8)
  387. ((u8 *)xfer->rx_buf)[elements++] = w;
  388. else if (word_len <= 16)
  389. ((u16 *)xfer->rx_buf)[elements++] = w;
  390. else /* word_len <= 32 */
  391. ((u32 *)xfer->rx_buf)[elements++] = w;
  392. } else {
  393. dev_err(&spi->dev,
  394. "DMA RX penultimate word empty");
  395. count -= (word_len <= 8) ? 2 :
  396. (word_len <= 16) ? 4 :
  397. /* word_len <= 32 */ 8;
  398. omap2_mcspi_set_enable(spi, 1);
  399. return count;
  400. }
  401. }
  402. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  403. & OMAP2_MCSPI_CHSTAT_RXS)) {
  404. u32 w;
  405. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  406. if (word_len <= 8)
  407. ((u8 *)xfer->rx_buf)[elements] = w;
  408. else if (word_len <= 16)
  409. ((u16 *)xfer->rx_buf)[elements] = w;
  410. else /* word_len <= 32 */
  411. ((u32 *)xfer->rx_buf)[elements] = w;
  412. } else {
  413. dev_err(&spi->dev, "DMA RX last word empty");
  414. count -= (word_len <= 8) ? 1 :
  415. (word_len <= 16) ? 2 :
  416. /* word_len <= 32 */ 4;
  417. }
  418. omap2_mcspi_set_enable(spi, 1);
  419. }
  420. return count;
  421. }
  422. static unsigned
  423. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  424. {
  425. struct omap2_mcspi *mcspi;
  426. struct omap2_mcspi_cs *cs = spi->controller_state;
  427. unsigned int count, c;
  428. u32 l;
  429. void __iomem *base = cs->base;
  430. void __iomem *tx_reg;
  431. void __iomem *rx_reg;
  432. void __iomem *chstat_reg;
  433. int word_len;
  434. mcspi = spi_master_get_devdata(spi->master);
  435. count = xfer->len;
  436. c = count;
  437. word_len = cs->word_len;
  438. l = mcspi_cached_chconf0(spi);
  439. /* We store the pre-calculated register addresses on stack to speed
  440. * up the transfer loop. */
  441. tx_reg = base + OMAP2_MCSPI_TX0;
  442. rx_reg = base + OMAP2_MCSPI_RX0;
  443. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  444. if (c < (word_len>>3))
  445. return 0;
  446. if (word_len <= 8) {
  447. u8 *rx;
  448. const u8 *tx;
  449. rx = xfer->rx_buf;
  450. tx = xfer->tx_buf;
  451. do {
  452. c -= 1;
  453. if (tx != NULL) {
  454. if (mcspi_wait_for_reg_bit(chstat_reg,
  455. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  456. dev_err(&spi->dev, "TXS timed out\n");
  457. goto out;
  458. }
  459. dev_vdbg(&spi->dev, "write-%d %02x\n",
  460. word_len, *tx);
  461. __raw_writel(*tx++, tx_reg);
  462. }
  463. if (rx != NULL) {
  464. if (mcspi_wait_for_reg_bit(chstat_reg,
  465. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  466. dev_err(&spi->dev, "RXS timed out\n");
  467. goto out;
  468. }
  469. if (c == 1 && tx == NULL &&
  470. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  471. omap2_mcspi_set_enable(spi, 0);
  472. *rx++ = __raw_readl(rx_reg);
  473. dev_vdbg(&spi->dev, "read-%d %02x\n",
  474. word_len, *(rx - 1));
  475. if (mcspi_wait_for_reg_bit(chstat_reg,
  476. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  477. dev_err(&spi->dev,
  478. "RXS timed out\n");
  479. goto out;
  480. }
  481. c = 0;
  482. } else if (c == 0 && tx == NULL) {
  483. omap2_mcspi_set_enable(spi, 0);
  484. }
  485. *rx++ = __raw_readl(rx_reg);
  486. dev_vdbg(&spi->dev, "read-%d %02x\n",
  487. word_len, *(rx - 1));
  488. }
  489. } while (c);
  490. } else if (word_len <= 16) {
  491. u16 *rx;
  492. const u16 *tx;
  493. rx = xfer->rx_buf;
  494. tx = xfer->tx_buf;
  495. do {
  496. c -= 2;
  497. if (tx != NULL) {
  498. if (mcspi_wait_for_reg_bit(chstat_reg,
  499. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  500. dev_err(&spi->dev, "TXS timed out\n");
  501. goto out;
  502. }
  503. dev_vdbg(&spi->dev, "write-%d %04x\n",
  504. word_len, *tx);
  505. __raw_writel(*tx++, tx_reg);
  506. }
  507. if (rx != NULL) {
  508. if (mcspi_wait_for_reg_bit(chstat_reg,
  509. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  510. dev_err(&spi->dev, "RXS timed out\n");
  511. goto out;
  512. }
  513. if (c == 2 && tx == NULL &&
  514. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  515. omap2_mcspi_set_enable(spi, 0);
  516. *rx++ = __raw_readl(rx_reg);
  517. dev_vdbg(&spi->dev, "read-%d %04x\n",
  518. word_len, *(rx - 1));
  519. if (mcspi_wait_for_reg_bit(chstat_reg,
  520. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  521. dev_err(&spi->dev,
  522. "RXS timed out\n");
  523. goto out;
  524. }
  525. c = 0;
  526. } else if (c == 0 && tx == NULL) {
  527. omap2_mcspi_set_enable(spi, 0);
  528. }
  529. *rx++ = __raw_readl(rx_reg);
  530. dev_vdbg(&spi->dev, "read-%d %04x\n",
  531. word_len, *(rx - 1));
  532. }
  533. } while (c >= 2);
  534. } else if (word_len <= 32) {
  535. u32 *rx;
  536. const u32 *tx;
  537. rx = xfer->rx_buf;
  538. tx = xfer->tx_buf;
  539. do {
  540. c -= 4;
  541. if (tx != NULL) {
  542. if (mcspi_wait_for_reg_bit(chstat_reg,
  543. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  544. dev_err(&spi->dev, "TXS timed out\n");
  545. goto out;
  546. }
  547. dev_vdbg(&spi->dev, "write-%d %08x\n",
  548. word_len, *tx);
  549. __raw_writel(*tx++, tx_reg);
  550. }
  551. if (rx != NULL) {
  552. if (mcspi_wait_for_reg_bit(chstat_reg,
  553. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  554. dev_err(&spi->dev, "RXS timed out\n");
  555. goto out;
  556. }
  557. if (c == 4 && tx == NULL &&
  558. (l & OMAP2_MCSPI_CHCONF_TURBO)) {
  559. omap2_mcspi_set_enable(spi, 0);
  560. *rx++ = __raw_readl(rx_reg);
  561. dev_vdbg(&spi->dev, "read-%d %08x\n",
  562. word_len, *(rx - 1));
  563. if (mcspi_wait_for_reg_bit(chstat_reg,
  564. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  565. dev_err(&spi->dev,
  566. "RXS timed out\n");
  567. goto out;
  568. }
  569. c = 0;
  570. } else if (c == 0 && tx == NULL) {
  571. omap2_mcspi_set_enable(spi, 0);
  572. }
  573. *rx++ = __raw_readl(rx_reg);
  574. dev_vdbg(&spi->dev, "read-%d %08x\n",
  575. word_len, *(rx - 1));
  576. }
  577. } while (c >= 4);
  578. }
  579. /* for TX_ONLY mode, be sure all words have shifted out */
  580. if (xfer->rx_buf == NULL) {
  581. if (mcspi_wait_for_reg_bit(chstat_reg,
  582. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  583. dev_err(&spi->dev, "TXS timed out\n");
  584. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  585. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  586. dev_err(&spi->dev, "EOT timed out\n");
  587. /* disable chan to purge rx datas received in TX_ONLY transfer,
  588. * otherwise these rx datas will affect the direct following
  589. * RX_ONLY transfer.
  590. */
  591. omap2_mcspi_set_enable(spi, 0);
  592. }
  593. out:
  594. omap2_mcspi_set_enable(spi, 1);
  595. return count - c;
  596. }
  597. static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
  598. {
  599. u32 div;
  600. for (div = 0; div < 15; div++)
  601. if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
  602. return div;
  603. return 15;
  604. }
  605. /* called only when no transfer is active to this device */
  606. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  607. struct spi_transfer *t)
  608. {
  609. struct omap2_mcspi_cs *cs = spi->controller_state;
  610. struct omap2_mcspi *mcspi;
  611. struct spi_master *spi_cntrl;
  612. u32 l = 0, div = 0;
  613. u8 word_len = spi->bits_per_word;
  614. u32 speed_hz = spi->max_speed_hz;
  615. mcspi = spi_master_get_devdata(spi->master);
  616. spi_cntrl = mcspi->master;
  617. if (t != NULL && t->bits_per_word)
  618. word_len = t->bits_per_word;
  619. cs->word_len = word_len;
  620. if (t && t->speed_hz)
  621. speed_hz = t->speed_hz;
  622. speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
  623. div = omap2_mcspi_calc_divisor(speed_hz);
  624. l = mcspi_cached_chconf0(spi);
  625. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  626. * REVISIT: this controller could support SPI_3WIRE mode.
  627. */
  628. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  629. l |= OMAP2_MCSPI_CHCONF_DPE0;
  630. /* wordlength */
  631. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  632. l |= (word_len - 1) << 7;
  633. /* set chipselect polarity; manage with FORCE */
  634. if (!(spi->mode & SPI_CS_HIGH))
  635. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  636. else
  637. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  638. /* set clock divisor */
  639. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  640. l |= div << 2;
  641. /* set SPI mode 0..3 */
  642. if (spi->mode & SPI_CPOL)
  643. l |= OMAP2_MCSPI_CHCONF_POL;
  644. else
  645. l &= ~OMAP2_MCSPI_CHCONF_POL;
  646. if (spi->mode & SPI_CPHA)
  647. l |= OMAP2_MCSPI_CHCONF_PHA;
  648. else
  649. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  650. mcspi_write_chconf0(spi, l);
  651. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  652. OMAP2_MCSPI_MAX_FREQ >> div,
  653. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  654. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  655. return 0;
  656. }
  657. static int omap2_mcspi_request_dma(struct spi_device *spi)
  658. {
  659. struct spi_master *master = spi->master;
  660. struct omap2_mcspi *mcspi;
  661. struct omap2_mcspi_dma *mcspi_dma;
  662. dma_cap_mask_t mask;
  663. unsigned sig;
  664. mcspi = spi_master_get_devdata(master);
  665. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  666. init_completion(&mcspi_dma->dma_rx_completion);
  667. init_completion(&mcspi_dma->dma_tx_completion);
  668. dma_cap_zero(mask);
  669. dma_cap_set(DMA_SLAVE, mask);
  670. sig = mcspi_dma->dma_rx_sync_dev;
  671. mcspi_dma->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  672. if (!mcspi_dma->dma_rx) {
  673. dev_err(&spi->dev, "no RX DMA engine channel for McSPI\n");
  674. return -EAGAIN;
  675. }
  676. sig = mcspi_dma->dma_tx_sync_dev;
  677. mcspi_dma->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  678. if (!mcspi_dma->dma_tx) {
  679. dev_err(&spi->dev, "no TX DMA engine channel for McSPI\n");
  680. dma_release_channel(mcspi_dma->dma_rx);
  681. mcspi_dma->dma_rx = NULL;
  682. return -EAGAIN;
  683. }
  684. return 0;
  685. }
  686. static int omap2_mcspi_setup(struct spi_device *spi)
  687. {
  688. int ret;
  689. struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
  690. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  691. struct omap2_mcspi_dma *mcspi_dma;
  692. struct omap2_mcspi_cs *cs = spi->controller_state;
  693. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  694. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  695. spi->bits_per_word);
  696. return -EINVAL;
  697. }
  698. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  699. if (!cs) {
  700. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  701. if (!cs)
  702. return -ENOMEM;
  703. cs->base = mcspi->base + spi->chip_select * 0x14;
  704. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  705. cs->chconf0 = 0;
  706. spi->controller_state = cs;
  707. /* Link this to context save list */
  708. list_add_tail(&cs->node, &ctx->cs);
  709. }
  710. if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
  711. ret = omap2_mcspi_request_dma(spi);
  712. if (ret < 0)
  713. return ret;
  714. }
  715. ret = omap2_mcspi_enable_clocks(mcspi);
  716. if (ret < 0)
  717. return ret;
  718. ret = omap2_mcspi_setup_transfer(spi, NULL);
  719. omap2_mcspi_disable_clocks(mcspi);
  720. return ret;
  721. }
  722. static void omap2_mcspi_cleanup(struct spi_device *spi)
  723. {
  724. struct omap2_mcspi *mcspi;
  725. struct omap2_mcspi_dma *mcspi_dma;
  726. struct omap2_mcspi_cs *cs;
  727. mcspi = spi_master_get_devdata(spi->master);
  728. if (spi->controller_state) {
  729. /* Unlink controller state from context save list */
  730. cs = spi->controller_state;
  731. list_del(&cs->node);
  732. kfree(cs);
  733. }
  734. if (spi->chip_select < spi->master->num_chipselect) {
  735. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  736. if (mcspi_dma->dma_rx) {
  737. dma_release_channel(mcspi_dma->dma_rx);
  738. mcspi_dma->dma_rx = NULL;
  739. }
  740. if (mcspi_dma->dma_tx) {
  741. dma_release_channel(mcspi_dma->dma_tx);
  742. mcspi_dma->dma_tx = NULL;
  743. }
  744. }
  745. }
  746. static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
  747. {
  748. /* We only enable one channel at a time -- the one whose message is
  749. * -- although this controller would gladly
  750. * arbitrate among multiple channels. This corresponds to "single
  751. * channel" master mode. As a side effect, we need to manage the
  752. * chipselect with the FORCE bit ... CS != channel enable.
  753. */
  754. struct spi_device *spi;
  755. struct spi_transfer *t = NULL;
  756. int cs_active = 0;
  757. struct omap2_mcspi_cs *cs;
  758. struct omap2_mcspi_device_config *cd;
  759. int par_override = 0;
  760. int status = 0;
  761. u32 chconf;
  762. spi = m->spi;
  763. cs = spi->controller_state;
  764. cd = spi->controller_data;
  765. omap2_mcspi_set_enable(spi, 1);
  766. list_for_each_entry(t, &m->transfers, transfer_list) {
  767. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  768. status = -EINVAL;
  769. break;
  770. }
  771. if (par_override || t->speed_hz || t->bits_per_word) {
  772. par_override = 1;
  773. status = omap2_mcspi_setup_transfer(spi, t);
  774. if (status < 0)
  775. break;
  776. if (!t->speed_hz && !t->bits_per_word)
  777. par_override = 0;
  778. }
  779. if (!cs_active) {
  780. omap2_mcspi_force_cs(spi, 1);
  781. cs_active = 1;
  782. }
  783. chconf = mcspi_cached_chconf0(spi);
  784. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  785. chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
  786. if (t->tx_buf == NULL)
  787. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  788. else if (t->rx_buf == NULL)
  789. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  790. if (cd && cd->turbo_mode && t->tx_buf == NULL) {
  791. /* Turbo mode is for more than one word */
  792. if (t->len > ((cs->word_len + 7) >> 3))
  793. chconf |= OMAP2_MCSPI_CHCONF_TURBO;
  794. }
  795. mcspi_write_chconf0(spi, chconf);
  796. if (t->len) {
  797. unsigned count;
  798. /* RX_ONLY mode needs dummy data in TX reg */
  799. if (t->tx_buf == NULL)
  800. __raw_writel(0, cs->base
  801. + OMAP2_MCSPI_TX0);
  802. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  803. count = omap2_mcspi_txrx_dma(spi, t);
  804. else
  805. count = omap2_mcspi_txrx_pio(spi, t);
  806. m->actual_length += count;
  807. if (count != t->len) {
  808. status = -EIO;
  809. break;
  810. }
  811. }
  812. if (t->delay_usecs)
  813. udelay(t->delay_usecs);
  814. /* ignore the "leave it on after last xfer" hint */
  815. if (t->cs_change) {
  816. omap2_mcspi_force_cs(spi, 0);
  817. cs_active = 0;
  818. }
  819. }
  820. /* Restore defaults if they were overriden */
  821. if (par_override) {
  822. par_override = 0;
  823. status = omap2_mcspi_setup_transfer(spi, NULL);
  824. }
  825. if (cs_active)
  826. omap2_mcspi_force_cs(spi, 0);
  827. omap2_mcspi_set_enable(spi, 0);
  828. m->status = status;
  829. }
  830. static int omap2_mcspi_transfer_one_message(struct spi_master *master,
  831. struct spi_message *m)
  832. {
  833. struct omap2_mcspi *mcspi;
  834. struct spi_transfer *t;
  835. mcspi = spi_master_get_devdata(master);
  836. m->actual_length = 0;
  837. m->status = 0;
  838. /* reject invalid messages and transfers */
  839. if (list_empty(&m->transfers))
  840. return -EINVAL;
  841. list_for_each_entry(t, &m->transfers, transfer_list) {
  842. const void *tx_buf = t->tx_buf;
  843. void *rx_buf = t->rx_buf;
  844. unsigned len = t->len;
  845. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  846. || (len && !(rx_buf || tx_buf))
  847. || (t->bits_per_word &&
  848. ( t->bits_per_word < 4
  849. || t->bits_per_word > 32))) {
  850. dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  851. t->speed_hz,
  852. len,
  853. tx_buf ? "tx" : "",
  854. rx_buf ? "rx" : "",
  855. t->bits_per_word);
  856. return -EINVAL;
  857. }
  858. if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
  859. dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
  860. t->speed_hz,
  861. OMAP2_MCSPI_MAX_FREQ >> 15);
  862. return -EINVAL;
  863. }
  864. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  865. continue;
  866. if (tx_buf != NULL) {
  867. t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
  868. len, DMA_TO_DEVICE);
  869. if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
  870. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  871. 'T', len);
  872. return -EINVAL;
  873. }
  874. }
  875. if (rx_buf != NULL) {
  876. t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
  877. DMA_FROM_DEVICE);
  878. if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
  879. dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
  880. 'R', len);
  881. if (tx_buf != NULL)
  882. dma_unmap_single(mcspi->dev, t->tx_dma,
  883. len, DMA_TO_DEVICE);
  884. return -EINVAL;
  885. }
  886. }
  887. }
  888. omap2_mcspi_work(mcspi, m);
  889. spi_finalize_current_message(master);
  890. return 0;
  891. }
  892. static int __devinit omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
  893. {
  894. struct spi_master *master = mcspi->master;
  895. struct omap2_mcspi_regs *ctx = &mcspi->ctx;
  896. int ret = 0;
  897. ret = omap2_mcspi_enable_clocks(mcspi);
  898. if (ret < 0)
  899. return ret;
  900. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
  901. OMAP2_MCSPI_WAKEUPENABLE_WKEN);
  902. ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  903. omap2_mcspi_set_master_mode(master);
  904. omap2_mcspi_disable_clocks(mcspi);
  905. return 0;
  906. }
  907. static int omap_mcspi_runtime_resume(struct device *dev)
  908. {
  909. struct omap2_mcspi *mcspi;
  910. struct spi_master *master;
  911. master = dev_get_drvdata(dev);
  912. mcspi = spi_master_get_devdata(master);
  913. omap2_mcspi_restore_ctx(mcspi);
  914. return 0;
  915. }
  916. static struct omap2_mcspi_platform_config omap2_pdata = {
  917. .regs_offset = 0,
  918. };
  919. static struct omap2_mcspi_platform_config omap4_pdata = {
  920. .regs_offset = OMAP4_MCSPI_REG_OFFSET,
  921. };
  922. static const struct of_device_id omap_mcspi_of_match[] = {
  923. {
  924. .compatible = "ti,omap2-mcspi",
  925. .data = &omap2_pdata,
  926. },
  927. {
  928. .compatible = "ti,omap4-mcspi",
  929. .data = &omap4_pdata,
  930. },
  931. { },
  932. };
  933. MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
  934. static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
  935. {
  936. struct spi_master *master;
  937. struct omap2_mcspi_platform_config *pdata;
  938. struct omap2_mcspi *mcspi;
  939. struct resource *r;
  940. int status = 0, i;
  941. u32 regs_offset = 0;
  942. static int bus_num = 1;
  943. struct device_node *node = pdev->dev.of_node;
  944. const struct of_device_id *match;
  945. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  946. if (master == NULL) {
  947. dev_dbg(&pdev->dev, "master allocation failed\n");
  948. return -ENOMEM;
  949. }
  950. /* the spi->mode bits understood by this driver: */
  951. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  952. master->setup = omap2_mcspi_setup;
  953. master->prepare_transfer_hardware = omap2_prepare_transfer;
  954. master->unprepare_transfer_hardware = omap2_unprepare_transfer;
  955. master->transfer_one_message = omap2_mcspi_transfer_one_message;
  956. master->cleanup = omap2_mcspi_cleanup;
  957. master->dev.of_node = node;
  958. match = of_match_device(omap_mcspi_of_match, &pdev->dev);
  959. if (match) {
  960. u32 num_cs = 1; /* default number of chipselect */
  961. pdata = match->data;
  962. of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
  963. master->num_chipselect = num_cs;
  964. master->bus_num = bus_num++;
  965. } else {
  966. pdata = pdev->dev.platform_data;
  967. master->num_chipselect = pdata->num_cs;
  968. if (pdev->id != -1)
  969. master->bus_num = pdev->id;
  970. }
  971. regs_offset = pdata->regs_offset;
  972. dev_set_drvdata(&pdev->dev, master);
  973. mcspi = spi_master_get_devdata(master);
  974. mcspi->master = master;
  975. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  976. if (r == NULL) {
  977. status = -ENODEV;
  978. goto free_master;
  979. }
  980. r->start += regs_offset;
  981. r->end += regs_offset;
  982. mcspi->phys = r->start;
  983. mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
  984. if (!mcspi->base) {
  985. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  986. status = -ENOMEM;
  987. goto free_master;
  988. }
  989. mcspi->dev = &pdev->dev;
  990. INIT_LIST_HEAD(&mcspi->ctx.cs);
  991. mcspi->dma_channels = kcalloc(master->num_chipselect,
  992. sizeof(struct omap2_mcspi_dma),
  993. GFP_KERNEL);
  994. if (mcspi->dma_channels == NULL)
  995. goto free_master;
  996. for (i = 0; i < master->num_chipselect; i++) {
  997. char dma_ch_name[14];
  998. struct resource *dma_res;
  999. sprintf(dma_ch_name, "rx%d", i);
  1000. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1001. dma_ch_name);
  1002. if (!dma_res) {
  1003. dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
  1004. status = -ENODEV;
  1005. break;
  1006. }
  1007. mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
  1008. sprintf(dma_ch_name, "tx%d", i);
  1009. dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
  1010. dma_ch_name);
  1011. if (!dma_res) {
  1012. dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
  1013. status = -ENODEV;
  1014. break;
  1015. }
  1016. mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
  1017. }
  1018. if (status < 0)
  1019. goto dma_chnl_free;
  1020. pm_runtime_use_autosuspend(&pdev->dev);
  1021. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  1022. pm_runtime_enable(&pdev->dev);
  1023. if (status || omap2_mcspi_master_setup(mcspi) < 0)
  1024. goto disable_pm;
  1025. status = spi_register_master(master);
  1026. if (status < 0)
  1027. goto disable_pm;
  1028. return status;
  1029. disable_pm:
  1030. pm_runtime_disable(&pdev->dev);
  1031. dma_chnl_free:
  1032. kfree(mcspi->dma_channels);
  1033. free_master:
  1034. spi_master_put(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");