omap2_mcspi.c 30 KB

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