omap2_mcspi.c 30 KB

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