spi-omap2-mcspi.c 33 KB

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