omap2_mcspi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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 <plat/dma.h>
  36. #include <plat/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. if (spi->controller_state) {
  630. /* Unlink controller state from context save list */
  631. cs = spi->controller_state;
  632. list_del(&cs->node);
  633. kfree(spi->controller_state);
  634. }
  635. if (mcspi_dma->dma_rx_channel != -1) {
  636. omap_free_dma(mcspi_dma->dma_rx_channel);
  637. mcspi_dma->dma_rx_channel = -1;
  638. }
  639. if (mcspi_dma->dma_tx_channel != -1) {
  640. omap_free_dma(mcspi_dma->dma_tx_channel);
  641. mcspi_dma->dma_tx_channel = -1;
  642. }
  643. }
  644. static void omap2_mcspi_work(struct work_struct *work)
  645. {
  646. struct omap2_mcspi *mcspi;
  647. mcspi = container_of(work, struct omap2_mcspi, work);
  648. spin_lock_irq(&mcspi->lock);
  649. if (omap2_mcspi_enable_clocks(mcspi))
  650. goto out;
  651. /* We only enable one channel at a time -- the one whose message is
  652. * at the head of the queue -- although this controller would gladly
  653. * arbitrate among multiple channels. This corresponds to "single
  654. * channel" master mode. As a side effect, we need to manage the
  655. * chipselect with the FORCE bit ... CS != channel enable.
  656. */
  657. while (!list_empty(&mcspi->msg_queue)) {
  658. struct spi_message *m;
  659. struct spi_device *spi;
  660. struct spi_transfer *t = NULL;
  661. int cs_active = 0;
  662. struct omap2_mcspi_cs *cs;
  663. int par_override = 0;
  664. int status = 0;
  665. u32 chconf;
  666. m = container_of(mcspi->msg_queue.next, struct spi_message,
  667. queue);
  668. list_del_init(&m->queue);
  669. spin_unlock_irq(&mcspi->lock);
  670. spi = m->spi;
  671. cs = spi->controller_state;
  672. omap2_mcspi_set_enable(spi, 1);
  673. list_for_each_entry(t, &m->transfers, transfer_list) {
  674. if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  675. status = -EINVAL;
  676. break;
  677. }
  678. if (par_override || t->speed_hz || t->bits_per_word) {
  679. par_override = 1;
  680. status = omap2_mcspi_setup_transfer(spi, t);
  681. if (status < 0)
  682. break;
  683. if (!t->speed_hz && !t->bits_per_word)
  684. par_override = 0;
  685. }
  686. if (!cs_active) {
  687. omap2_mcspi_force_cs(spi, 1);
  688. cs_active = 1;
  689. }
  690. chconf = mcspi_cached_chconf0(spi);
  691. chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
  692. if (t->tx_buf == NULL)
  693. chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
  694. else if (t->rx_buf == NULL)
  695. chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
  696. mcspi_write_chconf0(spi, chconf);
  697. if (t->len) {
  698. unsigned count;
  699. /* RX_ONLY mode needs dummy data in TX reg */
  700. if (t->tx_buf == NULL)
  701. __raw_writel(0, cs->base
  702. + OMAP2_MCSPI_TX0);
  703. if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
  704. count = omap2_mcspi_txrx_dma(spi, t);
  705. else
  706. count = omap2_mcspi_txrx_pio(spi, t);
  707. m->actual_length += count;
  708. if (count != t->len) {
  709. status = -EIO;
  710. break;
  711. }
  712. }
  713. if (t->delay_usecs)
  714. udelay(t->delay_usecs);
  715. /* ignore the "leave it on after last xfer" hint */
  716. if (t->cs_change) {
  717. omap2_mcspi_force_cs(spi, 0);
  718. cs_active = 0;
  719. }
  720. }
  721. /* Restore defaults if they were overriden */
  722. if (par_override) {
  723. par_override = 0;
  724. status = omap2_mcspi_setup_transfer(spi, NULL);
  725. }
  726. if (cs_active)
  727. omap2_mcspi_force_cs(spi, 0);
  728. omap2_mcspi_set_enable(spi, 0);
  729. m->status = status;
  730. m->complete(m->context);
  731. spin_lock_irq(&mcspi->lock);
  732. }
  733. omap2_mcspi_disable_clocks(mcspi);
  734. out:
  735. spin_unlock_irq(&mcspi->lock);
  736. }
  737. static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
  738. {
  739. struct omap2_mcspi *mcspi;
  740. unsigned long flags;
  741. struct spi_transfer *t;
  742. m->actual_length = 0;
  743. m->status = 0;
  744. /* reject invalid messages and transfers */
  745. if (list_empty(&m->transfers) || !m->complete)
  746. return -EINVAL;
  747. list_for_each_entry(t, &m->transfers, transfer_list) {
  748. const void *tx_buf = t->tx_buf;
  749. void *rx_buf = t->rx_buf;
  750. unsigned len = t->len;
  751. if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
  752. || (len && !(rx_buf || tx_buf))
  753. || (t->bits_per_word &&
  754. ( t->bits_per_word < 4
  755. || t->bits_per_word > 32))) {
  756. dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
  757. t->speed_hz,
  758. len,
  759. tx_buf ? "tx" : "",
  760. rx_buf ? "rx" : "",
  761. t->bits_per_word);
  762. return -EINVAL;
  763. }
  764. if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
  765. dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
  766. t->speed_hz,
  767. OMAP2_MCSPI_MAX_FREQ/(1<<16));
  768. return -EINVAL;
  769. }
  770. if (m->is_dma_mapped || len < DMA_MIN_BYTES)
  771. continue;
  772. /* Do DMA mapping "early" for better error reporting and
  773. * dcache use. Note that if dma_unmap_single() ever starts
  774. * to do real work on ARM, we'd need to clean up mappings
  775. * for previous transfers on *ALL* exits of this loop...
  776. */
  777. if (tx_buf != NULL) {
  778. t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
  779. len, DMA_TO_DEVICE);
  780. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  781. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  782. 'T', len);
  783. return -EINVAL;
  784. }
  785. }
  786. if (rx_buf != NULL) {
  787. t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
  788. DMA_FROM_DEVICE);
  789. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  790. dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
  791. 'R', len);
  792. if (tx_buf != NULL)
  793. dma_unmap_single(NULL, t->tx_dma,
  794. len, DMA_TO_DEVICE);
  795. return -EINVAL;
  796. }
  797. }
  798. }
  799. mcspi = spi_master_get_devdata(spi->master);
  800. spin_lock_irqsave(&mcspi->lock, flags);
  801. list_add_tail(&m->queue, &mcspi->msg_queue);
  802. queue_work(omap2_mcspi_wq, &mcspi->work);
  803. spin_unlock_irqrestore(&mcspi->lock, flags);
  804. return 0;
  805. }
  806. static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
  807. {
  808. struct spi_master *master = mcspi->master;
  809. u32 tmp;
  810. if (omap2_mcspi_enable_clocks(mcspi))
  811. return -1;
  812. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
  813. OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
  814. do {
  815. tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
  816. } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
  817. tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
  818. OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
  819. OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
  820. mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
  821. omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
  822. tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
  823. mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
  824. omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
  825. omap2_mcspi_set_master_mode(master);
  826. omap2_mcspi_disable_clocks(mcspi);
  827. return 0;
  828. }
  829. static u8 __initdata spi1_rxdma_id [] = {
  830. OMAP24XX_DMA_SPI1_RX0,
  831. OMAP24XX_DMA_SPI1_RX1,
  832. OMAP24XX_DMA_SPI1_RX2,
  833. OMAP24XX_DMA_SPI1_RX3,
  834. };
  835. static u8 __initdata spi1_txdma_id [] = {
  836. OMAP24XX_DMA_SPI1_TX0,
  837. OMAP24XX_DMA_SPI1_TX1,
  838. OMAP24XX_DMA_SPI1_TX2,
  839. OMAP24XX_DMA_SPI1_TX3,
  840. };
  841. static u8 __initdata spi2_rxdma_id[] = {
  842. OMAP24XX_DMA_SPI2_RX0,
  843. OMAP24XX_DMA_SPI2_RX1,
  844. };
  845. static u8 __initdata spi2_txdma_id[] = {
  846. OMAP24XX_DMA_SPI2_TX0,
  847. OMAP24XX_DMA_SPI2_TX1,
  848. };
  849. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  850. || defined(CONFIG_ARCH_OMAP4)
  851. static u8 __initdata spi3_rxdma_id[] = {
  852. OMAP24XX_DMA_SPI3_RX0,
  853. OMAP24XX_DMA_SPI3_RX1,
  854. };
  855. static u8 __initdata spi3_txdma_id[] = {
  856. OMAP24XX_DMA_SPI3_TX0,
  857. OMAP24XX_DMA_SPI3_TX1,
  858. };
  859. #endif
  860. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  861. static u8 __initdata spi4_rxdma_id[] = {
  862. OMAP34XX_DMA_SPI4_RX0,
  863. };
  864. static u8 __initdata spi4_txdma_id[] = {
  865. OMAP34XX_DMA_SPI4_TX0,
  866. };
  867. #endif
  868. static int __init omap2_mcspi_probe(struct platform_device *pdev)
  869. {
  870. struct spi_master *master;
  871. struct omap2_mcspi *mcspi;
  872. struct resource *r;
  873. int status = 0, i;
  874. const u8 *rxdma_id, *txdma_id;
  875. unsigned num_chipselect;
  876. switch (pdev->id) {
  877. case 1:
  878. rxdma_id = spi1_rxdma_id;
  879. txdma_id = spi1_txdma_id;
  880. num_chipselect = 4;
  881. break;
  882. case 2:
  883. rxdma_id = spi2_rxdma_id;
  884. txdma_id = spi2_txdma_id;
  885. num_chipselect = 2;
  886. break;
  887. #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
  888. || defined(CONFIG_ARCH_OMAP4)
  889. case 3:
  890. rxdma_id = spi3_rxdma_id;
  891. txdma_id = spi3_txdma_id;
  892. num_chipselect = 2;
  893. break;
  894. #endif
  895. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
  896. case 4:
  897. rxdma_id = spi4_rxdma_id;
  898. txdma_id = spi4_txdma_id;
  899. num_chipselect = 1;
  900. break;
  901. #endif
  902. default:
  903. return -EINVAL;
  904. }
  905. master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
  906. if (master == NULL) {
  907. dev_dbg(&pdev->dev, "master allocation failed\n");
  908. return -ENOMEM;
  909. }
  910. /* the spi->mode bits understood by this driver: */
  911. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  912. if (pdev->id != -1)
  913. master->bus_num = pdev->id;
  914. master->setup = omap2_mcspi_setup;
  915. master->transfer = omap2_mcspi_transfer;
  916. master->cleanup = omap2_mcspi_cleanup;
  917. master->num_chipselect = num_chipselect;
  918. dev_set_drvdata(&pdev->dev, master);
  919. mcspi = spi_master_get_devdata(master);
  920. mcspi->master = master;
  921. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  922. if (r == NULL) {
  923. status = -ENODEV;
  924. goto err1;
  925. }
  926. if (!request_mem_region(r->start, (r->end - r->start) + 1,
  927. dev_name(&pdev->dev))) {
  928. status = -EBUSY;
  929. goto err1;
  930. }
  931. mcspi->phys = r->start;
  932. mcspi->base = ioremap(r->start, r->end - r->start + 1);
  933. if (!mcspi->base) {
  934. dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
  935. status = -ENOMEM;
  936. goto err1aa;
  937. }
  938. INIT_WORK(&mcspi->work, omap2_mcspi_work);
  939. spin_lock_init(&mcspi->lock);
  940. INIT_LIST_HEAD(&mcspi->msg_queue);
  941. INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
  942. mcspi->ick = clk_get(&pdev->dev, "ick");
  943. if (IS_ERR(mcspi->ick)) {
  944. dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
  945. status = PTR_ERR(mcspi->ick);
  946. goto err1a;
  947. }
  948. mcspi->fck = clk_get(&pdev->dev, "fck");
  949. if (IS_ERR(mcspi->fck)) {
  950. dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
  951. status = PTR_ERR(mcspi->fck);
  952. goto err2;
  953. }
  954. mcspi->dma_channels = kcalloc(master->num_chipselect,
  955. sizeof(struct omap2_mcspi_dma),
  956. GFP_KERNEL);
  957. if (mcspi->dma_channels == NULL)
  958. goto err3;
  959. for (i = 0; i < num_chipselect; i++) {
  960. mcspi->dma_channels[i].dma_rx_channel = -1;
  961. mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
  962. mcspi->dma_channels[i].dma_tx_channel = -1;
  963. mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
  964. }
  965. if (omap2_mcspi_reset(mcspi) < 0)
  966. goto err4;
  967. status = spi_register_master(master);
  968. if (status < 0)
  969. goto err4;
  970. return status;
  971. err4:
  972. kfree(mcspi->dma_channels);
  973. err3:
  974. clk_put(mcspi->fck);
  975. err2:
  976. clk_put(mcspi->ick);
  977. err1a:
  978. iounmap(mcspi->base);
  979. err1aa:
  980. release_mem_region(r->start, (r->end - r->start) + 1);
  981. err1:
  982. spi_master_put(master);
  983. return status;
  984. }
  985. static int __exit omap2_mcspi_remove(struct platform_device *pdev)
  986. {
  987. struct spi_master *master;
  988. struct omap2_mcspi *mcspi;
  989. struct omap2_mcspi_dma *dma_channels;
  990. struct resource *r;
  991. void __iomem *base;
  992. master = dev_get_drvdata(&pdev->dev);
  993. mcspi = spi_master_get_devdata(master);
  994. dma_channels = mcspi->dma_channels;
  995. clk_put(mcspi->fck);
  996. clk_put(mcspi->ick);
  997. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  998. release_mem_region(r->start, (r->end - r->start) + 1);
  999. base = mcspi->base;
  1000. spi_unregister_master(master);
  1001. iounmap(base);
  1002. kfree(dma_channels);
  1003. return 0;
  1004. }
  1005. /* work with hotplug and coldplug */
  1006. MODULE_ALIAS("platform:omap2_mcspi");
  1007. static struct platform_driver omap2_mcspi_driver = {
  1008. .driver = {
  1009. .name = "omap2_mcspi",
  1010. .owner = THIS_MODULE,
  1011. },
  1012. .remove = __exit_p(omap2_mcspi_remove),
  1013. };
  1014. static int __init omap2_mcspi_init(void)
  1015. {
  1016. omap2_mcspi_wq = create_singlethread_workqueue(
  1017. omap2_mcspi_driver.driver.name);
  1018. if (omap2_mcspi_wq == NULL)
  1019. return -1;
  1020. return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
  1021. }
  1022. subsys_initcall(omap2_mcspi_init);
  1023. static void __exit omap2_mcspi_exit(void)
  1024. {
  1025. platform_driver_unregister(&omap2_mcspi_driver);
  1026. destroy_workqueue(omap2_mcspi_wq);
  1027. }
  1028. module_exit(omap2_mcspi_exit);
  1029. MODULE_LICENSE("GPL");