omap2_mcspi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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/spi/spi.h>
  36. #include <plat/dma.h>
  37. #include <plat/clock.h>
  38. #define OMAP2_MCSPI_MAX_FREQ 48000000
  39. /* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
  40. #define OMAP2_MCSPI_MAX_CTRL 4
  41. #define OMAP2_MCSPI_REVISION 0x00
  42. #define OMAP2_MCSPI_SYSCONFIG 0x10
  43. #define OMAP2_MCSPI_SYSSTATUS 0x14
  44. #define OMAP2_MCSPI_IRQSTATUS 0x18
  45. #define OMAP2_MCSPI_IRQENABLE 0x1c
  46. #define OMAP2_MCSPI_WAKEUPENABLE 0x20
  47. #define OMAP2_MCSPI_SYST 0x24
  48. #define OMAP2_MCSPI_MODULCTRL 0x28
  49. /* per-channel banks, 0x14 bytes each, first is: */
  50. #define OMAP2_MCSPI_CHCONF0 0x2c
  51. #define OMAP2_MCSPI_CHSTAT0 0x30
  52. #define OMAP2_MCSPI_CHCTRL0 0x34
  53. #define OMAP2_MCSPI_TX0 0x38
  54. #define OMAP2_MCSPI_RX0 0x3c
  55. /* per-register bitmasks: */
  56. #define OMAP2_MCSPI_SYSCONFIG_SMARTIDLE BIT(4)
  57. #define OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
  58. #define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
  59. #define OMAP2_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
  60. #define OMAP2_MCSPI_SYSSTATUS_RESETDONE BIT(0)
  61. #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
  62. #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
  63. #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
  64. #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
  65. #define OMAP2_MCSPI_CHCONF_POL BIT(1)
  66. #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
  67. #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
  68. #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
  69. #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  70. #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  71. #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
  72. #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
  73. #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
  74. #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
  75. #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
  76. #define OMAP2_MCSPI_CHCONF_IS BIT(18)
  77. #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
  78. #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
  79. #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
  80. #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
  81. #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
  82. #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
  83. #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  84. /* We have 2 DMA channels per CS, one for RX and one for TX */
  85. struct omap2_mcspi_dma {
  86. int dma_tx_channel;
  87. int dma_rx_channel;
  88. int dma_tx_sync_dev;
  89. int dma_rx_sync_dev;
  90. struct completion dma_tx_completion;
  91. struct completion dma_rx_completion;
  92. };
  93. /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
  94. * cache operations; better heuristics consider wordsize and bitrate.
  95. */
  96. #define DMA_MIN_BYTES 8
  97. struct omap2_mcspi {
  98. struct work_struct work;
  99. /* lock protects queue and registers */
  100. spinlock_t lock;
  101. struct list_head msg_queue;
  102. struct spi_master *master;
  103. struct clk *ick;
  104. struct clk *fck;
  105. /* Virtual base address of the controller */
  106. void __iomem *base;
  107. unsigned long phys;
  108. /* SPI1 has 4 channels, while SPI2 has 2 */
  109. struct omap2_mcspi_dma *dma_channels;
  110. };
  111. struct omap2_mcspi_cs {
  112. void __iomem *base;
  113. unsigned long phys;
  114. int word_len;
  115. struct list_head node;
  116. /* Context save and restore shadow register */
  117. u32 chconf0;
  118. };
  119. /* used for context save and restore, structure members to be updated whenever
  120. * corresponding registers are modified.
  121. */
  122. struct omap2_mcspi_regs {
  123. u32 sysconfig;
  124. u32 modulctrl;
  125. u32 wakeupenable;
  126. struct list_head cs;
  127. };
  128. static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
  129. static struct workqueue_struct *omap2_mcspi_wq;
  130. #define MOD_REG_BIT(val, mask, set) do { \
  131. if (set) \
  132. val |= mask; \
  133. else \
  134. val &= ~mask; \
  135. } while (0)
  136. static inline void mcspi_write_reg(struct spi_master *master,
  137. int idx, u32 val)
  138. {
  139. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  140. __raw_writel(val, mcspi->base + idx);
  141. }
  142. static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
  143. {
  144. struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
  145. return __raw_readl(mcspi->base + idx);
  146. }
  147. static inline void mcspi_write_cs_reg(const struct spi_device *spi,
  148. int idx, u32 val)
  149. {
  150. struct omap2_mcspi_cs *cs = spi->controller_state;
  151. __raw_writel(val, cs->base + idx);
  152. }
  153. static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
  154. {
  155. struct omap2_mcspi_cs *cs = spi->controller_state;
  156. return __raw_readl(cs->base + idx);
  157. }
  158. static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
  159. {
  160. struct omap2_mcspi_cs *cs = spi->controller_state;
  161. return cs->chconf0;
  162. }
  163. static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
  164. {
  165. struct omap2_mcspi_cs *cs = spi->controller_state;
  166. cs->chconf0 = val;
  167. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
  168. }
  169. static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
  170. int is_read, int enable)
  171. {
  172. u32 l, rw;
  173. l = mcspi_cached_chconf0(spi);
  174. if (is_read) /* 1 is read, 0 write */
  175. rw = OMAP2_MCSPI_CHCONF_DMAR;
  176. else
  177. rw = OMAP2_MCSPI_CHCONF_DMAW;
  178. MOD_REG_BIT(l, rw, enable);
  179. mcspi_write_chconf0(spi, l);
  180. }
  181. static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
  182. {
  183. u32 l;
  184. l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
  185. mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
  186. }
  187. static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
  188. {
  189. u32 l;
  190. l = mcspi_cached_chconf0(spi);
  191. MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
  192. mcspi_write_chconf0(spi, l);
  193. }
  194. static void omap2_mcspi_set_master_mode(struct spi_master *master)
  195. {
  196. u32 l;
  197. /* setup when switching from (reset default) slave mode
  198. * to single-channel master mode
  199. */
  200. l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
  201. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
  202. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
  203. MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
  204. mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
  205. omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
  206. }
  207. static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
  208. {
  209. struct spi_master *spi_cntrl;
  210. struct omap2_mcspi_cs *cs;
  211. spi_cntrl = mcspi->master;
  212. /* McSPI: context restore */
  213. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
  214. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
  215. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_SYSCONFIG,
  216. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].sysconfig);
  217. mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
  218. omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
  219. list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
  220. node)
  221. __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
  222. }
  223. static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
  224. {
  225. clk_disable(mcspi->ick);
  226. clk_disable(mcspi->fck);
  227. }
  228. static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
  229. {
  230. if (clk_enable(mcspi->ick))
  231. return -ENODEV;
  232. if (clk_enable(mcspi->fck))
  233. return -ENODEV;
  234. omap2_mcspi_restore_ctx(mcspi);
  235. return 0;
  236. }
  237. static unsigned
  238. omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
  239. {
  240. struct omap2_mcspi *mcspi;
  241. struct omap2_mcspi_cs *cs = spi->controller_state;
  242. struct omap2_mcspi_dma *mcspi_dma;
  243. unsigned int count, c;
  244. unsigned long base, tx_reg, rx_reg;
  245. int word_len, data_type, element_count;
  246. u8 * rx;
  247. const u8 * tx;
  248. mcspi = spi_master_get_devdata(spi->master);
  249. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  250. count = xfer->len;
  251. c = count;
  252. word_len = cs->word_len;
  253. base = cs->phys;
  254. tx_reg = base + OMAP2_MCSPI_TX0;
  255. rx_reg = base + OMAP2_MCSPI_RX0;
  256. rx = xfer->rx_buf;
  257. tx = xfer->tx_buf;
  258. if (word_len <= 8) {
  259. data_type = OMAP_DMA_DATA_TYPE_S8;
  260. element_count = count;
  261. } else if (word_len <= 16) {
  262. data_type = OMAP_DMA_DATA_TYPE_S16;
  263. element_count = count >> 1;
  264. } else /* word_len <= 32 */ {
  265. data_type = OMAP_DMA_DATA_TYPE_S32;
  266. element_count = count >> 2;
  267. }
  268. if (tx != NULL) {
  269. omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
  270. data_type, element_count, 1,
  271. OMAP_DMA_SYNC_ELEMENT,
  272. mcspi_dma->dma_tx_sync_dev, 0);
  273. omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
  274. OMAP_DMA_AMODE_CONSTANT,
  275. tx_reg, 0, 0);
  276. omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
  277. OMAP_DMA_AMODE_POST_INC,
  278. xfer->tx_dma, 0, 0);
  279. }
  280. if (rx != NULL) {
  281. omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
  282. data_type, element_count - 1, 1,
  283. OMAP_DMA_SYNC_ELEMENT,
  284. mcspi_dma->dma_rx_sync_dev, 1);
  285. omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
  286. OMAP_DMA_AMODE_CONSTANT,
  287. rx_reg, 0, 0);
  288. omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
  289. OMAP_DMA_AMODE_POST_INC,
  290. xfer->rx_dma, 0, 0);
  291. }
  292. if (tx != NULL) {
  293. omap_start_dma(mcspi_dma->dma_tx_channel);
  294. omap2_mcspi_set_dma_req(spi, 0, 1);
  295. }
  296. if (rx != NULL) {
  297. omap_start_dma(mcspi_dma->dma_rx_channel);
  298. omap2_mcspi_set_dma_req(spi, 1, 1);
  299. }
  300. if (tx != NULL) {
  301. wait_for_completion(&mcspi_dma->dma_tx_completion);
  302. dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
  303. }
  304. if (rx != NULL) {
  305. wait_for_completion(&mcspi_dma->dma_rx_completion);
  306. dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
  307. omap2_mcspi_set_enable(spi, 0);
  308. if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
  309. & OMAP2_MCSPI_CHSTAT_RXS)) {
  310. u32 w;
  311. w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
  312. if (word_len <= 8)
  313. ((u8 *)xfer->rx_buf)[element_count - 1] = w;
  314. else if (word_len <= 16)
  315. ((u16 *)xfer->rx_buf)[element_count - 1] = w;
  316. else /* word_len <= 32 */
  317. ((u32 *)xfer->rx_buf)[element_count - 1] = w;
  318. } else {
  319. dev_err(&spi->dev, "DMA RX last word empty");
  320. count -= (word_len <= 8) ? 1 :
  321. (word_len <= 16) ? 2 :
  322. /* word_len <= 32 */ 4;
  323. }
  324. omap2_mcspi_set_enable(spi, 1);
  325. }
  326. return count;
  327. }
  328. static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
  329. {
  330. unsigned long timeout;
  331. timeout = jiffies + msecs_to_jiffies(1000);
  332. while (!(__raw_readl(reg) & bit)) {
  333. if (time_after(jiffies, timeout))
  334. return -1;
  335. cpu_relax();
  336. }
  337. return 0;
  338. }
  339. static unsigned
  340. omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
  341. {
  342. struct omap2_mcspi *mcspi;
  343. struct omap2_mcspi_cs *cs = spi->controller_state;
  344. unsigned int count, c;
  345. u32 l;
  346. void __iomem *base = cs->base;
  347. void __iomem *tx_reg;
  348. void __iomem *rx_reg;
  349. void __iomem *chstat_reg;
  350. int word_len;
  351. mcspi = spi_master_get_devdata(spi->master);
  352. count = xfer->len;
  353. c = count;
  354. word_len = cs->word_len;
  355. l = mcspi_cached_chconf0(spi);
  356. l &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  357. /* We store the pre-calculated register addresses on stack to speed
  358. * up the transfer loop. */
  359. tx_reg = base + OMAP2_MCSPI_TX0;
  360. rx_reg = base + OMAP2_MCSPI_RX0;
  361. chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
  362. if (word_len <= 8) {
  363. u8 *rx;
  364. const u8 *tx;
  365. rx = xfer->rx_buf;
  366. tx = xfer->tx_buf;
  367. do {
  368. c -= 1;
  369. if (tx != NULL) {
  370. if (mcspi_wait_for_reg_bit(chstat_reg,
  371. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  372. dev_err(&spi->dev, "TXS timed out\n");
  373. goto out;
  374. }
  375. #ifdef VERBOSE
  376. dev_dbg(&spi->dev, "write-%d %02x\n",
  377. word_len, *tx);
  378. #endif
  379. __raw_writel(*tx++, tx_reg);
  380. }
  381. if (rx != NULL) {
  382. if (mcspi_wait_for_reg_bit(chstat_reg,
  383. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  384. dev_err(&spi->dev, "RXS timed out\n");
  385. goto out;
  386. }
  387. /* prevent last RX_ONLY read from triggering
  388. * more word i/o: switch to rx+tx
  389. */
  390. if (c == 0 && tx == NULL)
  391. mcspi_write_chconf0(spi, l);
  392. *rx++ = __raw_readl(rx_reg);
  393. #ifdef VERBOSE
  394. dev_dbg(&spi->dev, "read-%d %02x\n",
  395. word_len, *(rx - 1));
  396. #endif
  397. }
  398. } while (c);
  399. } else if (word_len <= 16) {
  400. u16 *rx;
  401. const u16 *tx;
  402. rx = xfer->rx_buf;
  403. tx = xfer->tx_buf;
  404. do {
  405. c -= 2;
  406. if (tx != NULL) {
  407. if (mcspi_wait_for_reg_bit(chstat_reg,
  408. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  409. dev_err(&spi->dev, "TXS timed out\n");
  410. goto out;
  411. }
  412. #ifdef VERBOSE
  413. dev_dbg(&spi->dev, "write-%d %04x\n",
  414. word_len, *tx);
  415. #endif
  416. __raw_writel(*tx++, tx_reg);
  417. }
  418. if (rx != NULL) {
  419. if (mcspi_wait_for_reg_bit(chstat_reg,
  420. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  421. dev_err(&spi->dev, "RXS timed out\n");
  422. goto out;
  423. }
  424. /* prevent last RX_ONLY read from triggering
  425. * more word i/o: switch to rx+tx
  426. */
  427. if (c == 0 && tx == NULL)
  428. mcspi_write_chconf0(spi, l);
  429. *rx++ = __raw_readl(rx_reg);
  430. #ifdef VERBOSE
  431. dev_dbg(&spi->dev, "read-%d %04x\n",
  432. word_len, *(rx - 1));
  433. #endif
  434. }
  435. } while (c);
  436. } else if (word_len <= 32) {
  437. u32 *rx;
  438. const u32 *tx;
  439. rx = xfer->rx_buf;
  440. tx = xfer->tx_buf;
  441. do {
  442. c -= 4;
  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. #ifdef VERBOSE
  450. dev_dbg(&spi->dev, "write-%d %04x\n",
  451. word_len, *tx);
  452. #endif
  453. __raw_writel(*tx++, tx_reg);
  454. }
  455. if (rx != NULL) {
  456. if (mcspi_wait_for_reg_bit(chstat_reg,
  457. OMAP2_MCSPI_CHSTAT_RXS) < 0) {
  458. dev_err(&spi->dev, "RXS timed out\n");
  459. goto out;
  460. }
  461. /* prevent last RX_ONLY read from triggering
  462. * more word i/o: switch to rx+tx
  463. */
  464. if (c == 0 && tx == NULL)
  465. mcspi_write_chconf0(spi, l);
  466. *rx++ = __raw_readl(rx_reg);
  467. #ifdef VERBOSE
  468. dev_dbg(&spi->dev, "read-%d %04x\n",
  469. word_len, *(rx - 1));
  470. #endif
  471. }
  472. } while (c);
  473. }
  474. /* for TX_ONLY mode, be sure all words have shifted out */
  475. if (xfer->rx_buf == NULL) {
  476. if (mcspi_wait_for_reg_bit(chstat_reg,
  477. OMAP2_MCSPI_CHSTAT_TXS) < 0) {
  478. dev_err(&spi->dev, "TXS timed out\n");
  479. } else if (mcspi_wait_for_reg_bit(chstat_reg,
  480. OMAP2_MCSPI_CHSTAT_EOT) < 0)
  481. dev_err(&spi->dev, "EOT timed out\n");
  482. }
  483. out:
  484. return count - c;
  485. }
  486. /* called only when no transfer is active to this device */
  487. static int omap2_mcspi_setup_transfer(struct spi_device *spi,
  488. struct spi_transfer *t)
  489. {
  490. struct omap2_mcspi_cs *cs = spi->controller_state;
  491. struct omap2_mcspi *mcspi;
  492. struct spi_master *spi_cntrl;
  493. u32 l = 0, div = 0;
  494. u8 word_len = spi->bits_per_word;
  495. u32 speed_hz = spi->max_speed_hz;
  496. mcspi = spi_master_get_devdata(spi->master);
  497. spi_cntrl = mcspi->master;
  498. if (t != NULL && t->bits_per_word)
  499. word_len = t->bits_per_word;
  500. cs->word_len = word_len;
  501. if (t && t->speed_hz)
  502. speed_hz = t->speed_hz;
  503. if (speed_hz) {
  504. while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
  505. > speed_hz)
  506. div++;
  507. } else
  508. div = 15;
  509. l = mcspi_cached_chconf0(spi);
  510. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  511. * REVISIT: this controller could support SPI_3WIRE mode.
  512. */
  513. l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
  514. l |= OMAP2_MCSPI_CHCONF_DPE0;
  515. /* wordlength */
  516. l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
  517. l |= (word_len - 1) << 7;
  518. /* set chipselect polarity; manage with FORCE */
  519. if (!(spi->mode & SPI_CS_HIGH))
  520. l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
  521. else
  522. l &= ~OMAP2_MCSPI_CHCONF_EPOL;
  523. /* set clock divisor */
  524. l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
  525. l |= div << 2;
  526. /* set SPI mode 0..3 */
  527. if (spi->mode & SPI_CPOL)
  528. l |= OMAP2_MCSPI_CHCONF_POL;
  529. else
  530. l &= ~OMAP2_MCSPI_CHCONF_POL;
  531. if (spi->mode & SPI_CPHA)
  532. l |= OMAP2_MCSPI_CHCONF_PHA;
  533. else
  534. l &= ~OMAP2_MCSPI_CHCONF_PHA;
  535. mcspi_write_chconf0(spi, l);
  536. dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
  537. OMAP2_MCSPI_MAX_FREQ / (1 << div),
  538. (spi->mode & SPI_CPHA) ? "trailing" : "leading",
  539. (spi->mode & SPI_CPOL) ? "inverted" : "normal");
  540. return 0;
  541. }
  542. static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
  543. {
  544. struct spi_device *spi = data;
  545. struct omap2_mcspi *mcspi;
  546. struct omap2_mcspi_dma *mcspi_dma;
  547. mcspi = spi_master_get_devdata(spi->master);
  548. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  549. complete(&mcspi_dma->dma_rx_completion);
  550. /* We must disable the DMA RX request */
  551. omap2_mcspi_set_dma_req(spi, 1, 0);
  552. }
  553. static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
  554. {
  555. struct spi_device *spi = data;
  556. struct omap2_mcspi *mcspi;
  557. struct omap2_mcspi_dma *mcspi_dma;
  558. mcspi = spi_master_get_devdata(spi->master);
  559. mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
  560. complete(&mcspi_dma->dma_tx_completion);
  561. /* We must disable the DMA TX request */
  562. omap2_mcspi_set_dma_req(spi, 0, 0);
  563. }
  564. static int omap2_mcspi_request_dma(struct spi_device *spi)
  565. {
  566. struct spi_master *master = spi->master;
  567. struct omap2_mcspi *mcspi;
  568. struct omap2_mcspi_dma *mcspi_dma;
  569. mcspi = spi_master_get_devdata(master);
  570. mcspi_dma = mcspi->dma_channels + spi->chip_select;
  571. if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
  572. omap2_mcspi_dma_rx_callback, spi,
  573. &mcspi_dma->dma_rx_channel)) {
  574. dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
  575. return -EAGAIN;
  576. }
  577. if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
  578. omap2_mcspi_dma_tx_callback, spi,
  579. &mcspi_dma->dma_tx_channel)) {
  580. omap_free_dma(mcspi_dma->dma_rx_channel);
  581. mcspi_dma->dma_rx_channel = -1;
  582. dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
  583. return -EAGAIN;
  584. }
  585. init_completion(&mcspi_dma->dma_rx_completion);
  586. init_completion(&mcspi_dma->dma_tx_completion);
  587. return 0;
  588. }
  589. static int omap2_mcspi_setup(struct spi_device *spi)
  590. {
  591. int ret;
  592. struct omap2_mcspi *mcspi;
  593. struct omap2_mcspi_dma *mcspi_dma;
  594. struct omap2_mcspi_cs *cs = spi->controller_state;
  595. if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
  596. dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
  597. spi->bits_per_word);
  598. return -EINVAL;
  599. }
  600. mcspi = spi_master_get_devdata(spi->master);
  601. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  602. if (!cs) {
  603. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  604. if (!cs)
  605. return -ENOMEM;
  606. cs->base = mcspi->base + spi->chip_select * 0x14;
  607. cs->phys = mcspi->phys + spi->chip_select * 0x14;
  608. cs->chconf0 = 0;
  609. spi->controller_state = cs;
  610. /* Link this to context save list */
  611. list_add_tail(&cs->node,
  612. &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
  613. }
  614. if (mcspi_dma->dma_rx_channel == -1
  615. || mcspi_dma->dma_tx_channel == -1) {
  616. ret = omap2_mcspi_request_dma(spi);
  617. if (ret < 0)
  618. return ret;
  619. }
  620. if (omap2_mcspi_enable_clocks(mcspi))
  621. return -ENODEV;
  622. ret = omap2_mcspi_setup_transfer(spi, NULL);
  623. omap2_mcspi_disable_clocks(mcspi);
  624. return ret;
  625. }
  626. static void omap2_mcspi_cleanup(struct spi_device *spi)
  627. {
  628. struct omap2_mcspi *mcspi;
  629. struct omap2_mcspi_dma *mcspi_dma;
  630. struct omap2_mcspi_cs *cs;
  631. mcspi = spi_master_get_devdata(spi->master);
  632. mcspi_dma = &mcspi->dma_channels[spi->chip_select];
  633. if (spi->controller_state) {
  634. /* Unlink controller state from context save list */
  635. cs = spi->controller_state;
  636. list_del(&cs->node);
  637. kfree(spi->controller_state);
  638. }
  639. if (mcspi_dma->dma_rx_channel != -1) {
  640. omap_free_dma(mcspi_dma->dma_rx_channel);
  641. mcspi_dma->dma_rx_channel = -1;
  642. }
  643. if (mcspi_dma->dma_tx_channel != -1) {
  644. omap_free_dma(mcspi_dma->dma_tx_channel);
  645. mcspi_dma->dma_tx_channel = -1;
  646. }
  647. }
  648. static void omap2_mcspi_work(struct work_struct *work)
  649. {
  650. struct omap2_mcspi *mcspi;
  651. mcspi = container_of(work, struct omap2_mcspi, work);
  652. spin_lock_irq(&mcspi->lock);
  653. if (omap2_mcspi_enable_clocks(mcspi))
  654. goto out;
  655. /* We only enable one channel at a time -- the one whose message is
  656. * at the head of the queue -- although this controller would gladly
  657. * arbitrate among multiple channels. This corresponds to "single
  658. * channel" master mode. As a side effect, we need to manage the
  659. * chipselect with the FORCE bit ... CS != channel enable.
  660. */
  661. while (!list_empty(&mcspi->msg_queue)) {
  662. struct spi_message *m;
  663. struct spi_device *spi;
  664. struct spi_transfer *t = NULL;
  665. int cs_active = 0;
  666. struct omap2_mcspi_cs *cs;
  667. int par_override = 0;
  668. int status = 0;
  669. u32 chconf;
  670. m = container_of(mcspi->msg_queue.next, struct spi_message,
  671. queue);
  672. list_del_init(&m->queue);
  673. spin_unlock_irq(&mcspi->lock);
  674. spi = m->spi;
  675. cs = spi->controller_state;
  676. omap2_mcspi_set_enable(spi, 1);
  677. list_for_each_entry(t, &m->transfers, transfer_list) {
  678. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  679. status = -EINVAL;
  680. break;
  681. }
  682. if (par_override || t->speed_hz || t->bits_per_word) {
  683. par_override = 1;
  684. status = omap2_mcspi_setup_transfer(spi, t);
  685. if (status < 0)
  686. break;
  687. if (!t->speed_hz && !t->bits_per_word)
  688. par_override = 0;
  689. }
  690. if (!cs_active) {
  691. omap2_mcspi_force_cs(spi, 1);
  692. cs_active = 1;
  693. }
  694. chconf = mcspi_cached_chconf0(spi);
  695. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  696. if (t->tx_buf == NULL)
  697. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  698. else if (t->rx_buf == NULL)
  699. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  700. mcspi_write_chconf0(spi, chconf);
  701. if (t->len) {
  702. unsigned count;
  703. /* RX_ONLY mode needs dummy data in TX reg */
  704. if (t->tx_buf == NULL)
  705. __raw_writel(0, cs->base
  706. + OMAP2_MCSPI_TX0);
  707. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  708. count = omap2_mcspi_txrx_dma(spi, t);
  709. else
  710. count = omap2_mcspi_txrx_pio(spi, t);
  711. m->actual_length += count;
  712. if (count != t->len) {
  713. status = -EIO;
  714. break;
  715. }
  716. }
  717. if (t->delay_usecs)
  718. udelay(t->delay_usecs);
  719. /* ignore the "leave it on after last xfer" hint */
  720. if (t->cs_change) {
  721. omap2_mcspi_force_cs(spi, 0);
  722. cs_active = 0;
  723. }
  724. }
  725. /* Restore defaults if they were overriden */
  726. if (par_override) {
  727. par_override = 0;
  728. status = omap2_mcspi_setup_transfer(spi, NULL);
  729. }
  730. if (cs_active)
  731. omap2_mcspi_force_cs(spi, 0);
  732. omap2_mcspi_set_enable(spi, 0);
  733. m->status = status;
  734. m->complete(m->context);
  735. spin_lock_irq(&mcspi->lock);
  736. }
  737. omap2_mcspi_disable_clocks(mcspi);
  738. out:
  739. spin_unlock_irq(&mcspi->lock);
  740. }
  741. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  742. {
  743. struct omap2_mcspi *mcspi;
  744. unsigned long flags;
  745. struct spi_transfer *t;
  746. m->actual_length = 0;
  747. m->status = 0;
  748. /* reject invalid messages and transfers */
  749. if (list_empty(&m->transfers) || !m->complete)
  750. return -EINVAL;
  751. list_for_each_entry(t, &m->transfers, transfer_list) {
  752. const void *tx_buf = t->tx_buf;
  753. void *rx_buf = t->rx_buf;
  754. unsigned len = t->len;
  755. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  756. || (len && !(rx_buf || tx_buf))
  757. || (t->bits_per_word &&
  758. ( t->bits_per_word < 4
  759. || t->bits_per_word > 32))) {
  760. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  761. t->speed_hz,
  762. len,
  763. tx_buf ? "tx" : "",
  764. rx_buf ? "rx" : "",
  765. t->bits_per_word);
  766. return -EINVAL;
  767. }
  768. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  769. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  770. t->speed_hz,
  771. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  772. return -EINVAL;
  773. }
  774. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  775. continue;
  776. /* Do DMA mapping "early" for better error reporting and
  777. * dcache use. Note that if dma_unmap_single() ever starts
  778. * to do real work on ARM, we'd need to clean up mappings
  779. * for previous transfers on *ALL* exits of this loop...
  780. */
  781. if (tx_buf != NULL) {
  782. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  783. len, DMA_TO_DEVICE);
  784. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  785. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  786. 'T', len);
  787. return -EINVAL;
  788. }
  789. }
  790. if (rx_buf != NULL) {
  791. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  792. DMA_FROM_DEVICE);
  793. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  794. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  795. 'R', len);
  796. if (tx_buf != NULL)
  797. dma_unmap_single(NULL, t->tx_dma,
  798. len, DMA_TO_DEVICE);
  799. return -EINVAL;
  800. }
  801. }
  802. }
  803. mcspi = spi_master_get_devdata(spi->master);
  804. spin_lock_irqsave(&mcspi->lock, flags);
  805. list_add_tail(&m->queue, &mcspi->msg_queue);
  806. queue_work(omap2_mcspi_wq, &mcspi->work);
  807. spin_unlock_irqrestore(&mcspi->lock, flags);
  808. return 0;
  809. }
  810. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  811. {
  812. struct spi_master *master = mcspi->master;
  813. u32 tmp;
  814. if (omap2_mcspi_enable_clocks(mcspi))
  815. return -1;
  816. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  817. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  818. do {
  819. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  820. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  821. tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
  822. OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
  823. OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
  824. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
  825. omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
  826. tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  827. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
  828. omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
  829. omap2_mcspi_set_master_mode(master);
  830. omap2_mcspi_disable_clocks(mcspi);
  831. return 0;
  832. }
  833. static u8 __initdata spi1_rxdma_id [] = {
  834. OMAP24XX_DMA_SPI1_RX0,
  835. OMAP24XX_DMA_SPI1_RX1,
  836. OMAP24XX_DMA_SPI1_RX2,
  837. OMAP24XX_DMA_SPI1_RX3,
  838. };
  839. static u8 __initdata spi1_txdma_id [] = {
  840. OMAP24XX_DMA_SPI1_TX0,
  841. OMAP24XX_DMA_SPI1_TX1,
  842. OMAP24XX_DMA_SPI1_TX2,
  843. OMAP24XX_DMA_SPI1_TX3,
  844. };
  845. static u8 __initdata spi2_rxdma_id[] = {
  846. OMAP24XX_DMA_SPI2_RX0,
  847. OMAP24XX_DMA_SPI2_RX1,
  848. };
  849. static u8 __initdata spi2_txdma_id[] = {
  850. OMAP24XX_DMA_SPI2_TX0,
  851. OMAP24XX_DMA_SPI2_TX1,
  852. };
  853. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  854. || defined(CONFIG_ARCH_OMAP4)
  855. static u8 __initdata spi3_rxdma_id[] = {
  856. OMAP24XX_DMA_SPI3_RX0,
  857. OMAP24XX_DMA_SPI3_RX1,
  858. };
  859. static u8 __initdata spi3_txdma_id[] = {
  860. OMAP24XX_DMA_SPI3_TX0,
  861. OMAP24XX_DMA_SPI3_TX1,
  862. };
  863. #endif
  864. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  865. static u8 __initdata spi4_rxdma_id[] = {
  866. OMAP34XX_DMA_SPI4_RX0,
  867. };
  868. static u8 __initdata spi4_txdma_id[] = {
  869. OMAP34XX_DMA_SPI4_TX0,
  870. };
  871. #endif
  872. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  873. {
  874. struct spi_master *master;
  875. struct omap2_mcspi *mcspi;
  876. struct resource *r;
  877. int status = 0, i;
  878. const u8 *rxdma_id, *txdma_id;
  879. unsigned num_chipselect;
  880. switch (pdev->id) {
  881. case 1:
  882. rxdma_id = spi1_rxdma_id;
  883. txdma_id = spi1_txdma_id;
  884. num_chipselect = 4;
  885. break;
  886. case 2:
  887. rxdma_id = spi2_rxdma_id;
  888. txdma_id = spi2_txdma_id;
  889. num_chipselect = 2;
  890. break;
  891. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  892. || defined(CONFIG_ARCH_OMAP4)
  893. case 3:
  894. rxdma_id = spi3_rxdma_id;
  895. txdma_id = spi3_txdma_id;
  896. num_chipselect = 2;
  897. break;
  898. #endif
  899. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  900. case 4:
  901. rxdma_id = spi4_rxdma_id;
  902. txdma_id = spi4_txdma_id;
  903. num_chipselect = 1;
  904. break;
  905. #endif
  906. default:
  907. return -EINVAL;
  908. }
  909. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  910. if (master == NULL) {
  911. dev_dbg(&pdev->dev, "master allocation failed\n");
  912. return -ENOMEM;
  913. }
  914. /* the spi->mode bits understood by this driver: */
  915. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  916. if (pdev->id != -1)
  917. master->bus_num = pdev->id;
  918. master->setup = omap2_mcspi_setup;
  919. master->transfer = omap2_mcspi_transfer;
  920. master->cleanup = omap2_mcspi_cleanup;
  921. master->num_chipselect = num_chipselect;
  922. dev_set_drvdata(&pdev->dev, master);
  923. mcspi = spi_master_get_devdata(master);
  924. mcspi->master = master;
  925. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  926. if (r == NULL) {
  927. status = -ENODEV;
  928. goto err1;
  929. }
  930. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  931. dev_name(&pdev->dev))) {
  932. status = -EBUSY;
  933. goto err1;
  934. }
  935. mcspi->phys = r->start;
  936. mcspi->base = ioremap(r->start, r->end - r->start + 1);
  937. if (!mcspi->base) {
  938. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  939. status = -ENOMEM;
  940. goto err1aa;
  941. }
  942. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  943. spin_lock_init(&mcspi->lock);
  944. INIT_LIST_HEAD(&mcspi->msg_queue);
  945. INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
  946. mcspi->ick = clk_get(&pdev->dev, "ick");
  947. if (IS_ERR(mcspi->ick)) {
  948. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  949. status = PTR_ERR(mcspi->ick);
  950. goto err1a;
  951. }
  952. mcspi->fck = clk_get(&pdev->dev, "fck");
  953. if (IS_ERR(mcspi->fck)) {
  954. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  955. status = PTR_ERR(mcspi->fck);
  956. goto err2;
  957. }
  958. mcspi->dma_channels = kcalloc(master->num_chipselect,
  959. sizeof(struct omap2_mcspi_dma),
  960. GFP_KERNEL);
  961. if (mcspi->dma_channels == NULL)
  962. goto err3;
  963. for (i = 0; i < num_chipselect; i++) {
  964. mcspi->dma_channels[i].dma_rx_channel = -1;
  965. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  966. mcspi->dma_channels[i].dma_tx_channel = -1;
  967. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  968. }
  969. if (omap2_mcspi_reset(mcspi) < 0)
  970. goto err4;
  971. status = spi_register_master(master);
  972. if (status < 0)
  973. goto err4;
  974. return status;
  975. err4:
  976. kfree(mcspi->dma_channels);
  977. err3:
  978. clk_put(mcspi->fck);
  979. err2:
  980. clk_put(mcspi->ick);
  981. err1a:
  982. iounmap(mcspi->base);
  983. err1aa:
  984. release_mem_region(r->start, (r->end - r->start) + 1);
  985. err1:
  986. spi_master_put(master);
  987. return status;
  988. }
  989. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  990. {
  991. struct spi_master *master;
  992. struct omap2_mcspi *mcspi;
  993. struct omap2_mcspi_dma *dma_channels;
  994. struct resource *r;
  995. void __iomem *base;
  996. master = dev_get_drvdata(&pdev->dev);
  997. mcspi = spi_master_get_devdata(master);
  998. dma_channels = mcspi->dma_channels;
  999. clk_put(mcspi->fck);
  1000. clk_put(mcspi->ick);
  1001. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1002. release_mem_region(r->start, (r->end - r->start) + 1);
  1003. base = mcspi->base;
  1004. spi_unregister_master(master);
  1005. iounmap(base);
  1006. kfree(dma_channels);
  1007. return 0;
  1008. }
  1009. /* work with hotplug and coldplug */
  1010. MODULE_ALIAS("platform:omap2_mcspi");
  1011. static struct platform_driver omap2_mcspi_driver = {
  1012. .driver = {
  1013. .name = "omap2_mcspi",
  1014. .owner = THIS_MODULE,
  1015. },
  1016. .remove = __exit_p(omap2_mcspi_remove),
  1017. };
  1018. static int __init omap2_mcspi_init(void)
  1019. {
  1020. omap2_mcspi_wq = create_singlethread_workqueue(
  1021. omap2_mcspi_driver.driver.name);
  1022. if (omap2_mcspi_wq == NULL)
  1023. return -1;
  1024. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  1025. }
  1026. subsys_initcall(omap2_mcspi_init);
  1027. static void __exit omap2_mcspi_exit(void)
  1028. {
  1029. platform_driver_unregister(&omap2_mcspi_driver);
  1030. destroy_workqueue(omap2_mcspi_wq);
  1031. }
  1032. module_exit(omap2_mcspi_exit);
  1033. MODULE_LICENSE("GPL");