080-mcfv4e_dspi_update.patch 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. From 771f4741de2cd8a103d96cd1e8281926448ff56d Mon Sep 17 00:00:00 2001
  2. From: Kurt Mahan <[email protected]>
  3. Date: Tue, 15 Jul 2008 11:44:08 -0600
  4. Subject: [PATCH] Fix DSPI driver.
  5. Update to latest Matt Coldfire driver. Fix timing issues with
  6. M547x/M548x END-OF-QUEUE notification.
  7. LTIBName: mcfv4e-dspi-update
  8. Signed-off-by: Kurt Mahan <[email protected]>
  9. ---
  10. arch/m68k/coldfire/m547x_8x-devices.c | 35 +-
  11. drivers/spi/Kconfig | 6 +
  12. drivers/spi/Makefile | 2 +-
  13. drivers/spi/dspi_mcf.c | 1217 +++++++++++++++++++++++++++++++++
  14. include/asm-m68k/mcfdspi.h | 48 ++
  15. 5 files changed, 1296 insertions(+), 12 deletions(-)
  16. create mode 100644 drivers/spi/dspi_mcf.c
  17. create mode 100644 include/asm-m68k/mcfdspi.h
  18. --- a/arch/m68k/coldfire/m547x_8x-devices.c
  19. +++ b/arch/m68k/coldfire/m547x_8x-devices.c
  20. @@ -26,26 +26,40 @@
  21. */
  22. /* number of supported SPI selects */
  23. -#define SPI_NUM_CHIPSELECTS 4
  24. +#define SPI_NUM_CHIPSELECTS 8
  25. void coldfire_spi_cs_control(u8 cs, u8 command)
  26. {
  27. /* nothing special required */
  28. }
  29. +#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)
  30. +static struct coldfire_spi_chip spidev_chip_info = {
  31. + .bits_per_word = 8,
  32. +};
  33. +#endif
  34. +
  35. static struct spi_board_info spi_board_info[] = {
  36. - /* no board info */
  37. +#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)
  38. + {
  39. + .modalias = "spidev",
  40. + .max_speed_hz = 16000000, /* max clk (SCK) speed in HZ */
  41. + .bus_num = 1,
  42. + .chip_select = 0, /* CS0 */
  43. + .controller_data = &spidev_chip_info,
  44. + }
  45. +#endif
  46. };
  47. static int spi_irq_list[] = {
  48. - /* IRQ, ICR Offset, ICR Val, Mask */
  49. - 64 + ISC_DSPI_OVRFW, 0, 0, 0,
  50. - 64 + ISC_DSPI_RFOF, 0, 0, 0,
  51. - 64 + ISC_DSPI_RFDF, 0, 0, 0,
  52. - 64 + ISC_DSPI_TFUF, 0, 0, 0,
  53. - 64 + ISC_DSPI_TCF, 0, 0, 0,
  54. - 64 + ISC_DSPI_TFFF, 0, 0, 0,
  55. - 64 + ISC_DSPI_EOQF, 0, 0, 0,
  56. + /* IRQ, ICR Offset, ICR Val,Mask */
  57. + 64 + ISC_DSPI_OVRFW, ISC_DSPI_OVRFW, 0x18, 0,
  58. + 64 + ISC_DSPI_RFOF, ISC_DSPI_RFOF, 0x18, 0,
  59. + 64 + ISC_DSPI_RFDF, ISC_DSPI_RFDF, 0x18, 0,
  60. + 64 + ISC_DSPI_TFUF, ISC_DSPI_TFUF, 0x18, 0,
  61. + 64 + ISC_DSPI_TCF, ISC_DSPI_TCF, 0x18, 0,
  62. + 64 + ISC_DSPI_TFFF, ISC_DSPI_TFFF, 0x18, 0,
  63. + 64 + ISC_DSPI_EOQF, ISC_DSPI_EOQF, 0x18, 0,
  64. 0,0,0,0,
  65. };
  66. @@ -120,7 +134,6 @@ static int __init m547x_8x_spi_init(void
  67. /* register device */
  68. retval = platform_device_register(&coldfire_spi);
  69. if (retval < 0) {
  70. - printk(KERN_ERR "SPI-m547x_8x: platform_device_register failed with code=%d\n", retval);
  71. goto out;
  72. }
  73. --- a/drivers/spi/Kconfig
  74. +++ b/drivers/spi/Kconfig
  75. @@ -130,6 +130,12 @@ config SPI_COLDFIRE
  76. Tested with the 5282 processor, but should also work with other
  77. Coldfire variants.
  78. +config SPI_DSPI
  79. + tristate "Coldfire DSPI"
  80. + depends on SPI_MASTER && COLDFIRE
  81. + help
  82. + SPI driver for Coldfire DSPI driver only.
  83. +
  84. config SPI_COLDFIRE_DSPI_EDMA
  85. boolean "Coldfire DSPI master driver uses eDMA"
  86. depends on SPI_MASTER && COLDFIRE && SPI_COLDFIRE && EXPERIMENTAL && COLDFIRE_EDMA
  87. --- a/drivers/spi/Makefile
  88. +++ b/drivers/spi/Makefile
  89. @@ -19,7 +19,7 @@ obj-$(CONFIG_SPI_BITBANG) += spi_bitban
  90. obj-$(CONFIG_SPI_AU1550) += au1550_spi.o
  91. obj-$(CONFIG_SPI_BUTTERFLY) += spi_butterfly.o
  92. # obj-$(CONFIG_SPI_COLDFIRE) += spi_coldfire.o spi-m5445x.o
  93. -obj-$(CONFIG_SPI_COLDFIRE) += spi_coldfire.o
  94. +obj-$(CONFIG_SPI_DSPI) += dspi_mcf.o
  95. obj-$(CONFIG_SPI_GPIO) += spi_gpio.o
  96. obj-$(CONFIG_SPI_IMX) += spi_imx.o
  97. obj-$(CONFIG_SPI_LM70_LLP) += spi_lm70llp.o
  98. --- /dev/null
  99. +++ b/drivers/spi/dspi_mcf.c
  100. @@ -0,0 +1,1217 @@
  101. +/*
  102. + * dspi_mcf.c - DSPI controller for ColdFire processors
  103. + *
  104. + *
  105. + * Matt Waddel [email protected]
  106. + * Copyright Freescale Semiconductor, Inc. 2008
  107. + *
  108. + * M547x/M548x changes by Kurt Mahan [email protected]
  109. + * Copyright Freescale Semiconductor, Inc. 2008
  110. + *
  111. + * Based on spi_coldfire.c done by:
  112. + * Andrey Butok
  113. + * Yaroslav Vinogradov
  114. + * Copyright Freescale Semiconductor, Inc. 2006-2007
  115. + * Mike Lavender (mike@steroidmicros)
  116. + * (C) Copyright 2005, Intec Automation,
  117. + *
  118. + * This program is free software; you can redistribute it and/or modify it
  119. + * under the terms of the GNU General Public License as published by the
  120. + * Free Software Foundation; either version 2 of the License, or (at your
  121. + * option) any later version.
  122. + *
  123. + * This program is distributed in the hope that it will be useful,
  124. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  125. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  126. + * GNU General Public License for more details.
  127. + *
  128. + * You should have received a copy of the GNU General Public License
  129. + * along with this program; if not, write to the Free Software
  130. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  131. + *
  132. + ***************************************************************************
  133. + * Changes:
  134. + * v0.002 M547x/M548x support.
  135. + * v0.001 Initial version. Coldfire DSPI master driver.
  136. + ****************************************************************************/
  137. +
  138. +/*
  139. + * Includes
  140. + */
  141. +
  142. +#include <linux/autoconf.h>
  143. +#include <linux/init.h>
  144. +#include <linux/module.h>
  145. +#include <linux/device.h>
  146. +#include <linux/interrupt.h>
  147. +#include <linux/platform_device.h>
  148. +#include <linux/spi/spi.h>
  149. +#include <linux/workqueue.h>
  150. +#include <linux/delay.h>
  151. +#include <asm/mcfsim.h>
  152. +#include <asm/mcfqspi.h>
  153. +#include <asm/coldfire.h>
  154. +#include <linux/io.h>
  155. +#include <asm/mcfdspi.h>
  156. +#include <linux/dma-mapping.h>
  157. +
  158. +#undef DSPI_COLDFIRE_DEBUG
  159. +
  160. +#ifdef DSPI_COLDFIRE_DEBUG
  161. +#define DBG(fmt, args...) \
  162. + printk(KERN_INFO "[%s] " fmt , __FUNCTION__, ## args)
  163. +#else
  164. +#define DBG(fmt, args...) do {} while (0)
  165. +#endif
  166. +
  167. +#if defined(CONFIG_M54455)
  168. +#include <asm/mcf5445x_dspi.h>
  169. +#if defined(CONFIG_SPI_COLDFIRE_DSPI_EDMA)
  170. + #include <asm/mcf5445x_edma.h>
  171. +#endif
  172. +#endif
  173. +
  174. +#if defined(CONFIG_M547X_8X)
  175. +#include <asm/virtconvert.h>
  176. +#include <asm/m5485dspi.h>
  177. +#endif
  178. +
  179. +#if defined(CONFIG_SPI_COLDFIRE_DSPI_EDMA)
  180. +#include <asm/mcf_edma.h>
  181. +#define SPI_DSPI_EDMA
  182. +#define EDMA_BUFSIZE_KMALLOC (DSPI_FIFO_SIZE*4)
  183. +#define DSPI_DMA_RX_TCD MCF_EDMA_CHAN_DSPI_RX
  184. +#define DSPI_DMA_TX_TCD MCF_EDMA_CHAN_DSPI_TX
  185. +#endif
  186. +
  187. +#define DSPI_BITS MCF_DSPI_DCTAR_FMSZ(0xF)
  188. +#define DSPI_BITS_16 MCF_DSPI_DCTAR_FMSZ(0xF)
  189. +#define DSPI_BITS_8 MCF_DSPI_DCTAR_FMSZ(0x7)
  190. +#define DSPI_FIFO_SIZE 16
  191. +
  192. +#define DRIVER_NAME "Coldfire DSPI"
  193. +
  194. +/****************************************************************************/
  195. +
  196. +/*
  197. + * Local constants and macros
  198. + */
  199. +
  200. +#define START_STATE ((void *)0)
  201. +#define RUNNING_STATE ((void *)1)
  202. +#define DONE_STATE ((void *)2)
  203. +#define ERROR_STATE ((void *)-1)
  204. +
  205. +#define QUEUE_RUNNING 0
  206. +#define QUEUE_STOPPED 1
  207. +
  208. +/****************************************************************************/
  209. +
  210. +/*
  211. + * Local Data Structures
  212. + */
  213. +
  214. +struct DSPI_MCR {
  215. + unsigned master:1;
  216. + unsigned cont_scke:1;
  217. + unsigned dconf:2;
  218. + unsigned frz:1;
  219. + unsigned mtfe:1;
  220. + unsigned pcsse:1;
  221. + unsigned rooe:1;
  222. + unsigned pcsis:8;
  223. + unsigned reserved15:1;
  224. + unsigned mdis:1;
  225. + unsigned dis_tx:1;
  226. + unsigned dis_rxf:1;
  227. + unsigned clr_tx:1;
  228. + unsigned clr_rxf:1;
  229. + unsigned smpl_pt:2;
  230. + unsigned reserved71:7;
  231. + unsigned halt:1;
  232. +};
  233. +
  234. +struct DSPI_CTAR {
  235. + unsigned dbr:1;
  236. + unsigned fmsz:4;
  237. + unsigned cpol:1;
  238. + unsigned cpha:1;
  239. + unsigned lsbfe:1;
  240. + unsigned pcssck:2;
  241. + unsigned pasc:2;
  242. + unsigned pdt:2;
  243. + unsigned pbr:2;
  244. + unsigned cssck:4;
  245. + unsigned asc:4;
  246. + unsigned dt:4;
  247. + unsigned br:4;
  248. +};
  249. +
  250. +struct chip_data {
  251. + /* dspi data */
  252. + union {
  253. + u32 mcr_val;
  254. + struct DSPI_MCR mcr;
  255. + };
  256. + union {
  257. + u32 ctar_val;
  258. + struct DSPI_CTAR ctar;
  259. + };
  260. + u16 void_write_data;
  261. +};
  262. +
  263. +
  264. +struct driver_data {
  265. + /* Driver model hookup */
  266. + struct platform_device *pdev;
  267. +
  268. + /* SPI framework hookup */
  269. + struct spi_master *master;
  270. +
  271. + /* Driver message queue */
  272. + struct workqueue_struct *workqueue;
  273. + struct work_struct pump_messages;
  274. + spinlock_t lock; /* lock */
  275. + struct list_head queue;
  276. + int busy;
  277. + int run;
  278. +
  279. + /* Message Transfer pump */
  280. + struct tasklet_struct pump_transfers;
  281. +
  282. + /* Current message transfer state info */
  283. + struct spi_message *cur_msg;
  284. + struct spi_transfer *cur_transfer;
  285. + struct chip_data *cur_chip;
  286. + size_t len;
  287. + void *tx;
  288. + void *tx_end;
  289. + void *rx;
  290. + void *rx_end;
  291. + char flags;
  292. +#define TRAN_STATE_RX_VOID 0x01
  293. +#define TRAN_STATE_TX_VOID 0x02
  294. +#define TRAN_STATE_WORD_ODD_NUM 0x04
  295. + u8 cs;
  296. + u16 void_write_data;
  297. + unsigned cs_change:1;
  298. +
  299. + u32 trans_cnt;
  300. + u32 wce_cnt;
  301. + u32 abrt_cnt;
  302. + u32 *mcr; /* DSPI MCR register */
  303. + u32 *ctar; /* DSPI CTAR register */
  304. + u32 *dspi_dtfr; /* DSPI DTFR register */
  305. + u32 *dspi_drfr; /* DSPI DRFR register */
  306. + u32 *dspi_rser; /* DSPI RSER register */
  307. + u32 *dspi_sr; /* DSPI status register */
  308. +
  309. +#if defined(SPI_DSPI_EDMA)
  310. + void *edma_tx_buf;
  311. + void *edma_rx_buf;
  312. +#endif
  313. +
  314. +#if defined(CONFIG_M532x) || defined(CONFIG_M537x)
  315. + u16 *par; /* Pin assignment register */
  316. +#else
  317. + u8 *par; /* Pin assignment register */
  318. +#endif
  319. + u8 *int_icr; /* Interrupt level and priority register */
  320. + u32 *int_mr; /* Interrupt mask register */
  321. + void (*cs_control)(u8 cs, u8 command);
  322. +};
  323. +
  324. +#define DSPI_CS(cs) ((1<<(cs))<<16)
  325. +
  326. +/****************************************************************************/
  327. +
  328. +/*
  329. + * SPI local functions
  330. + */
  331. +
  332. +static void *next_transfer(struct driver_data *drv_data)
  333. +{
  334. + struct spi_message *msg = drv_data->cur_msg;
  335. + struct spi_transfer *trans = drv_data->cur_transfer;
  336. +
  337. + DBG("\n");
  338. + /* Move to next transfer */
  339. + if (trans->transfer_list.next != &msg->transfers) {
  340. + drv_data->cur_transfer = list_entry(trans->transfer_list.next,
  341. + struct spi_transfer,
  342. + transfer_list);
  343. +
  344. + if (drv_data->cur_transfer->transfer_list.next
  345. + == &msg->transfers) /* last transfer */
  346. + drv_data->cur_transfer->cs_change = 1;
  347. +
  348. + return RUNNING_STATE;
  349. + } else
  350. + return DONE_STATE;
  351. +}
  352. +
  353. +
  354. +static inline int is_word_transfer(struct driver_data *drv_data)
  355. +{
  356. + return ((*(drv_data->ctar+drv_data->cs) & DSPI_BITS_16) ==
  357. + DSPI_BITS_8) ? 0 : 1;
  358. +}
  359. +
  360. +static inline void set_8bit_transfer_mode(struct driver_data *drv_data)
  361. +{
  362. + DBG("\n");
  363. + *(drv_data->ctar+drv_data->cs) =
  364. + (*(drv_data->ctar + drv_data->cs) & ~DSPI_BITS) | DSPI_BITS_8;
  365. +}
  366. +
  367. +static inline void set_16bit_transfer_mode(struct driver_data *drv_data)
  368. +{
  369. + DBG("\n");
  370. + *(drv_data->ctar+drv_data->cs) =
  371. + (*(drv_data->ctar + drv_data->cs) & ~DSPI_BITS) | DSPI_BITS_16;
  372. +}
  373. +
  374. +static unsigned char hz_to_spi_baud(int pbr, int dbr, int speed_hz)
  375. +{
  376. + int pbr_tbl[4] = {2, 3, 5, 7}; /* Valid baud rate pre-scaler values */
  377. + int brs[16] = { 2, 4, 6, 8,
  378. + 16, 32, 64, 128,
  379. + 256, 512, 1024, 2048,
  380. + 4096, 8192, 16384, 32768 };
  381. + int temp, index = 0;
  382. +
  383. + if ((pbr < 0) || (pbr > 3) ||
  384. + (dbr < 0) || (dbr > 1))
  385. + return 15; /* table indexes out of range, go slow */
  386. +
  387. + temp = ((((MCF_CLK / 2) / pbr_tbl[pbr]) * (1 + dbr)) / speed_hz);
  388. +
  389. + while (temp >= brs[index])
  390. + if (index++ >= 15)
  391. + break;
  392. +
  393. + DBG("baud rate scaler = 0x%x - %d\n", index, brs[index]);
  394. + return(index);
  395. +}
  396. +
  397. +static int write(struct driver_data *drv_data)
  398. +{
  399. + int tx_count = 0;
  400. + int tx_word = is_word_transfer(drv_data);
  401. + u16 d16;
  402. + u8 d8;
  403. + u32 dspi_pushr = 0;
  404. + int first = 1;
  405. +#if defined(SPI_DSPI_EDMA)
  406. + u32 *edma_wr = (u32 *)(drv_data->edma_tx_buf);
  407. +#endif
  408. +
  409. + /* If we are in word mode, but only have a single byte to transfer
  410. + * then switch to byte mode temporarily. Will switch back at the
  411. + * end of the transfer. */
  412. + if (tx_word && ((drv_data->tx_end - drv_data->tx) == 1)) {
  413. + drv_data->flags |= TRAN_STATE_WORD_ODD_NUM;
  414. + set_8bit_transfer_mode(drv_data);
  415. + tx_word = 0;
  416. + }
  417. +
  418. + while ((drv_data->tx < drv_data->tx_end) && (tx_count < DSPI_FIFO_SIZE)) {
  419. +// DBG("while\n");
  420. + if (tx_word) {
  421. + if ((drv_data->tx_end - drv_data->tx) == 1)
  422. + break;
  423. +
  424. + if (!(drv_data->flags & TRAN_STATE_TX_VOID))
  425. + d16 = *(u16 *)drv_data->tx;
  426. + else
  427. + d16 = drv_data->void_write_data;
  428. +
  429. + dspi_pushr = MCF_DSPI_DTFR_TXDATA(d16) |
  430. + DSPI_CS(drv_data->cs) |
  431. + MCF_DSPI_DTFR_CTAS(drv_data->cs) |
  432. + MCF_DSPI_DTFR_CONT;
  433. +
  434. + drv_data->tx += 2;
  435. + } else {
  436. + if (!(drv_data->flags & TRAN_STATE_TX_VOID))
  437. + d8 = *(u8 *)drv_data->tx;
  438. + else
  439. + d8 = *(u8 *)&drv_data->void_write_data;
  440. +
  441. + dspi_pushr = MCF_DSPI_DTFR_TXDATA(d8) |
  442. + DSPI_CS(drv_data->cs) |
  443. + MCF_DSPI_DTFR_CTAS(drv_data->cs) |
  444. + MCF_DSPI_DTFR_CONT;
  445. +
  446. + drv_data->tx++;
  447. + }
  448. +
  449. + if (drv_data->tx == drv_data->tx_end
  450. + || tx_count == DSPI_FIFO_SIZE-1) {
  451. + /* last transfer in the queue */
  452. + dspi_pushr |= MCF_DSPI_DTFR_EOQ;
  453. + if ((drv_data->cs_change)
  454. + && (drv_data->tx == drv_data->tx_end))
  455. + dspi_pushr &= ~MCF_DSPI_DTFR_CONT;
  456. +#ifdef CONFIG_M547X_8X
  457. + /* EOQ gets missed if we don't delay */
  458. + udelay(100);
  459. +#endif
  460. + } else if (tx_word && ((drv_data->tx_end - drv_data->tx) == 1))
  461. + dspi_pushr |= MCF_DSPI_DTFR_EOQ;
  462. +
  463. + if (first) {
  464. + first = 0;
  465. + dspi_pushr |= MCF_DSPI_DTFR_CTCNT; /* clear counter */
  466. + }
  467. +#if defined(SPI_DSPI_EDMA)
  468. + *edma_wr = dspi_pushr;
  469. + edma_wr++;
  470. +#else
  471. + *drv_data->dspi_dtfr = dspi_pushr;
  472. +#endif
  473. + tx_count++;
  474. + }
  475. +
  476. +#if defined(SPI_DSPI_EDMA)
  477. + if (tx_count > 0) {
  478. +
  479. + mcf_edma_set_tcd_params(DSPI_DMA_TX_TCD,
  480. + virt_to_phys(drv_data->edma_tx_buf),
  481. + (u32)drv_data->dspi_dtfr,
  482. + MCF_EDMA_TCD_ATTR_SSIZE_32BIT
  483. + | MCF_EDMA_TCD_ATTR_DSIZE_32BIT,
  484. + 4, /* soff */
  485. + 4 * tx_count, /* nbytes */
  486. + 0, /* slast */
  487. + 1, /* citer */
  488. + 1, /* biter */
  489. + 0, /* doff */
  490. + 0, /* dlastsga */
  491. + 0, /* major_int */
  492. + 1); /* disable_req */
  493. +
  494. + mcf_edma_set_tcd_params(DSPI_DMA_RX_TCD,
  495. + (u32)drv_data->dspi_drfr,
  496. + virt_to_phys(drv_data->edma_rx_buf),
  497. + MCF_EDMA_TCD_ATTR_SSIZE_32BIT
  498. + | MCF_EDMA_TCD_ATTR_DSIZE_32BIT,
  499. + 0, /* soff */
  500. + 4 * tx_count, /* nbytes */
  501. + 0, /* slast */
  502. + 1, /* citer */
  503. + 1, /* biter */
  504. + 4, /* doff */
  505. + 0, /* dlastsga */
  506. + 0, /* major_int */
  507. + 1); /* disable_req */
  508. +
  509. + mcf_edma_start_transfer(DSPI_DMA_TX_TCD);
  510. + }
  511. +#endif
  512. + return (tx_count * (tx_word + 1));
  513. +}
  514. +
  515. +static int read(struct driver_data *drv_data)
  516. +{
  517. + int rx_count = 0;
  518. + int rx_word = is_word_transfer(drv_data);
  519. + u16 d;
  520. +#if defined(SPI_DSPI_EDMA)
  521. + u32 *rx_edma = (u32 *) drv_data->edma_rx_buf;
  522. +
  523. + /* receive SPI data */
  524. + mcf_edma_start_transfer(DSPI_DMA_RX_TCD);
  525. +#endif
  526. + while ((drv_data->rx < drv_data->rx_end)
  527. + && (rx_count < DSPI_FIFO_SIZE)) {
  528. +
  529. + if (rx_word) {
  530. + if ((drv_data->rx_end - drv_data->rx) == 1)
  531. + break;
  532. +#if defined(SPI_DSPI_EDMA)
  533. + d = MCF_DSPI_DRFR_RXDATA(*rx_edma);
  534. + rx_edma++;
  535. +#else
  536. + d = MCF_DSPI_DRFR_RXDATA(*drv_data->dspi_drfr);
  537. +#endif
  538. + if (!(drv_data->flags & TRAN_STATE_RX_VOID))
  539. + *(u16 *)drv_data->rx = d;
  540. + drv_data->rx += 2;
  541. +
  542. + } else {
  543. +#if defined(SPI_DSPI_EDMA)
  544. + d = MCF_DSPI_DRFR_RXDATA(*rx_edma);
  545. + rx_edma++;
  546. +#else
  547. + d = MCF_DSPI_DRFR_RXDATA(*drv_data->dspi_drfr);
  548. +#endif
  549. + if (!(drv_data->flags & TRAN_STATE_RX_VOID))
  550. + *(u8 *)drv_data->rx = d;
  551. + drv_data->rx++;
  552. + }
  553. + rx_count++;
  554. + DBG("rxd=0x%x\n", d);
  555. + }
  556. + return rx_count;
  557. +}
  558. +
  559. +
  560. +static inline void dspi_setup_chip(struct driver_data *drv_data)
  561. +{
  562. + struct chip_data *chip = drv_data->cur_chip;
  563. +
  564. + DBG("\n");
  565. + *drv_data->mcr = chip->mcr_val;
  566. + *(drv_data->ctar+drv_data->cs) = chip->ctar_val;
  567. + *drv_data->dspi_rser = MCF_DSPI_DRSER_EOQFE;
  568. +}
  569. +
  570. +#if defined(SPI_DSPI_EDMA)
  571. +static int edma_tx_handler(int channel, void *dev)
  572. +{
  573. + DBG("\n");
  574. + if (channel == DSPI_DMA_TX_TCD)
  575. + mcf_edma_stop_transfer(DSPI_DMA_TX_TCD);
  576. + return IRQ_HANDLED;
  577. +}
  578. +
  579. +static int edma_rx_handler(int channel, void *dev)
  580. +{
  581. + DBG("\n");
  582. + if (channel == DSPI_DMA_RX_TCD)
  583. + mcf_edma_stop_transfer(DSPI_DMA_RX_TCD);
  584. + return IRQ_HANDLED;
  585. +}
  586. +#endif
  587. +
  588. +static irqreturn_t dspi_interrupt(int irq, void *dev_id)
  589. +{
  590. + struct driver_data *drv_data = (struct driver_data *)dev_id;
  591. + struct spi_message *msg = drv_data->cur_msg;
  592. +
  593. + /* Clear all flags immediately */
  594. + *drv_data->dspi_sr = MCF_DSPI_DSR_EOQF;
  595. +
  596. + if (!drv_data->cur_msg || !drv_data->cur_msg->state) {
  597. +#if !defined(SPI_DSPI_EDMA)
  598. + u32 irq_status = *drv_data->dspi_sr;
  599. + /* if eDMA is used it happens some time (at least once)*/
  600. + printk(KERN_ERR "Bad message or transfer state handler. \
  601. + IRQ status = %x\n", irq_status);
  602. +#endif
  603. + return IRQ_NONE;
  604. + }
  605. +
  606. + DBG("\n");
  607. + /*
  608. + * Read the data into the buffer and reload and start
  609. + * queue with new data if not finished. If finished
  610. + * then setup the next transfer
  611. + */
  612. + read(drv_data);
  613. +
  614. + if (drv_data->rx == drv_data->rx_end) {
  615. + /*
  616. + * Finished now - fall through and schedule next
  617. + * transfer tasklet
  618. + */
  619. + if (drv_data->flags & TRAN_STATE_WORD_ODD_NUM)
  620. + set_16bit_transfer_mode(drv_data);
  621. +
  622. + msg->state = next_transfer(drv_data);
  623. + } else {
  624. + /* not finished yet - keep going */
  625. + msg->actual_length += write(drv_data);
  626. + return IRQ_HANDLED;
  627. + }
  628. +
  629. + tasklet_schedule(&drv_data->pump_transfers);
  630. +
  631. + return IRQ_HANDLED;
  632. +}
  633. +
  634. +/* caller already set message->status; dma and pio irqs are blocked */
  635. +static void giveback(struct driver_data *drv_data)
  636. +{
  637. + struct spi_transfer *last_transfer;
  638. + unsigned long flags;
  639. + struct spi_message *msg;
  640. + DBG("\n");
  641. +
  642. + spin_lock_irqsave(&drv_data->lock, flags);
  643. + msg = drv_data->cur_msg;
  644. + drv_data->cur_msg = NULL;
  645. + drv_data->cur_transfer = NULL;
  646. + drv_data->cur_chip = NULL;
  647. + queue_work(drv_data->workqueue, &drv_data->pump_messages);
  648. + spin_unlock_irqrestore(&drv_data->lock, flags);
  649. +
  650. + last_transfer = list_entry(msg->transfers.prev,
  651. + struct spi_transfer, transfer_list);
  652. +
  653. + if (!last_transfer->cs_change)
  654. + drv_data->cs_control(drv_data->cs, QSPI_CS_DROP);
  655. +
  656. + msg->state = NULL;
  657. + if (msg->complete)
  658. + msg->complete(msg->context);
  659. +}
  660. +
  661. +
  662. +static void pump_transfers(unsigned long data)
  663. +{
  664. + struct driver_data *drv_data = (struct driver_data *)data;
  665. + struct spi_message *message = NULL;
  666. + struct spi_transfer *transfer = NULL;
  667. + struct spi_transfer *previous = NULL;
  668. + struct chip_data *chip = NULL;
  669. + unsigned long flags;
  670. + DBG("\n");
  671. + /* Get current state information */
  672. + message = drv_data->cur_msg;
  673. + transfer = drv_data->cur_transfer;
  674. + chip = drv_data->cur_chip;
  675. +
  676. + /* Handle for abort */
  677. + if (message->state == ERROR_STATE) {
  678. + message->status = -EIO;
  679. + giveback(drv_data);
  680. + return;
  681. + }
  682. +
  683. + /* Handle end of message */
  684. + if (message->state == DONE_STATE) {
  685. + message->status = 0;
  686. + giveback(drv_data);
  687. + return;
  688. + }
  689. +
  690. + if (message->state == START_STATE) {
  691. + dspi_setup_chip(drv_data);
  692. +
  693. + if (drv_data->cs_control)
  694. + drv_data->cs_control(message->spi->chip_select,
  695. + QSPI_CS_ASSERT);
  696. + }
  697. +
  698. + /* Delay if requested at end of transfer*/
  699. + if (message->state == RUNNING_STATE) {
  700. + previous = list_entry(transfer->transfer_list.prev,
  701. + struct spi_transfer,
  702. + transfer_list);
  703. +
  704. + if (drv_data->cs_control && transfer->cs_change)
  705. + drv_data->cs_control(message->spi->chip_select,
  706. + QSPI_CS_DROP);
  707. +
  708. + if (previous->delay_usecs)
  709. + udelay(previous->delay_usecs);
  710. +
  711. + if (drv_data->cs_control && transfer->cs_change)
  712. + drv_data->cs_control(message->spi->chip_select,
  713. + QSPI_CS_ASSERT);
  714. + }
  715. +
  716. + drv_data->flags = 0;
  717. + drv_data->tx = (void *)transfer->tx_buf;
  718. + drv_data->tx_end = drv_data->tx + transfer->len;
  719. + drv_data->rx = transfer->rx_buf;
  720. + drv_data->rx_end = drv_data->rx + transfer->len;
  721. +
  722. + if (!drv_data->rx)
  723. + drv_data->flags |= TRAN_STATE_RX_VOID;
  724. + if (!drv_data->tx)
  725. + drv_data->flags |= TRAN_STATE_TX_VOID;
  726. + drv_data->cs = message->spi->chip_select;
  727. + drv_data->cs_change = transfer->cs_change;
  728. + drv_data->void_write_data = chip->void_write_data;
  729. + if (transfer->speed_hz) {
  730. + *(drv_data->ctar + drv_data->cs) = \
  731. + ((chip->ctar_val & ~0xF) | \
  732. + hz_to_spi_baud(chip->ctar.pbr, \
  733. + chip->ctar.dbr, \
  734. + transfer->speed_hz));
  735. + }
  736. +
  737. + message->state = RUNNING_STATE;
  738. + /* Go baby, go */
  739. + local_irq_save(flags);
  740. + message->actual_length += write(drv_data);
  741. + local_irq_restore(flags);
  742. +}
  743. +
  744. +
  745. +static void pump_messages(struct work_struct *work)
  746. +{
  747. + struct driver_data *drv_data;
  748. + unsigned long flags;
  749. + DBG("\n");
  750. +
  751. + drv_data = container_of(work, struct driver_data, pump_messages);
  752. +
  753. + /* Lock queue and check for queue work */
  754. + spin_lock_irqsave(&drv_data->lock, flags);
  755. + if (list_empty(&drv_data->queue)
  756. + || drv_data->run == QUEUE_STOPPED) {
  757. + drv_data->busy = 0;
  758. + spin_unlock_irqrestore(&drv_data->lock, flags);
  759. + return;
  760. + }
  761. +
  762. + /* Make sure we are not already running a message */
  763. + if (drv_data->cur_msg) {
  764. + spin_unlock_irqrestore(&drv_data->lock, flags);
  765. + return;
  766. + }
  767. +
  768. + /* Extract head of queue */
  769. + drv_data->cur_msg = list_entry(drv_data->queue.next,
  770. + struct spi_message, queue);
  771. + list_del_init(&drv_data->cur_msg->queue);
  772. +
  773. + /* Initial message state*/
  774. + drv_data->cur_msg->state = START_STATE;
  775. + drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  776. + struct spi_transfer,
  777. + transfer_list);
  778. +
  779. + if (drv_data->cur_transfer->transfer_list.next
  780. + == &drv_data->cur_msg->transfers)
  781. + drv_data->cur_transfer->cs_change = 1; /* last */
  782. +
  783. +
  784. + /* Setup the SPI Registers using the per chip configuration */
  785. + drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  786. +
  787. + /* Mark as busy and launch transfers */
  788. + tasklet_schedule(&drv_data->pump_transfers);
  789. +
  790. + drv_data->busy = 1;
  791. + spin_unlock_irqrestore(&drv_data->lock, flags);
  792. +}
  793. +
  794. +/****************************************************************************/
  795. +
  796. +/*
  797. + * SPI master implementation
  798. + */
  799. +
  800. +static int transfer(struct spi_device *spi, struct spi_message *msg)
  801. +{
  802. + struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  803. + unsigned long flags;
  804. +
  805. + DBG("\n");
  806. + spin_lock_irqsave(&drv_data->lock, flags);
  807. +
  808. + if (drv_data->run == QUEUE_STOPPED) {
  809. + spin_unlock_irqrestore(&drv_data->lock, flags);
  810. + return -ESHUTDOWN;
  811. + }
  812. +
  813. + msg->actual_length = 0;
  814. + msg->status = -EINPROGRESS;
  815. + msg->state = START_STATE;
  816. +
  817. + list_add_tail(&msg->queue, &drv_data->queue);
  818. +
  819. + if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
  820. + queue_work(drv_data->workqueue, &drv_data->pump_messages);
  821. +
  822. + spin_unlock_irqrestore(&drv_data->lock, flags);
  823. +
  824. + return 0;
  825. +}
  826. +
  827. +
  828. +static int setup(struct spi_device *spi)
  829. +{
  830. +
  831. + struct chip_data *chip;
  832. + struct coldfire_dspi_chip *chip_info
  833. + = (struct coldfire_dspi_chip *)spi->controller_data;
  834. + DBG("\n");
  835. +
  836. + /* Only alloc on first setup */
  837. + chip = spi_get_ctldata(spi);
  838. + if (chip == NULL) {
  839. + chip = kcalloc(1, sizeof(struct chip_data), GFP_KERNEL);
  840. + if (!chip)
  841. + return -ENOMEM;
  842. + spi->mode = chip_info->mode;
  843. + spi->bits_per_word = chip_info->bits_per_word;
  844. + }
  845. +
  846. + chip->mcr.master = 1;
  847. + chip->mcr.cont_scke = 0;
  848. + chip->mcr.dconf = 0;
  849. + chip->mcr.frz = 0;
  850. + chip->mcr.mtfe = 0;
  851. + chip->mcr.pcsse = 0;
  852. + chip->mcr.rooe = 0;
  853. + chip->mcr.pcsis = 0xFF;
  854. + chip->mcr.reserved15 = 0;
  855. + chip->mcr.mdis = 0;
  856. + chip->mcr.dis_tx = 0;
  857. + chip->mcr.dis_rxf = 0;
  858. + chip->mcr.clr_tx = 1;
  859. + chip->mcr.clr_rxf = 1;
  860. + chip->mcr.smpl_pt = 0;
  861. + chip->mcr.reserved71 = 0;
  862. + chip->mcr.halt = 0;
  863. +
  864. + if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
  865. + chip->ctar.fmsz = spi->bits_per_word-1;
  866. + } else {
  867. + printk(KERN_ERR "Invalid wordsize\n");
  868. + kfree(chip);
  869. + return -ENODEV;
  870. + }
  871. +
  872. + chip->void_write_data = chip_info->void_write_data;
  873. +
  874. + if (spi->max_speed_hz != 0)
  875. + chip_info->br = hz_to_spi_baud(chip_info->pbr, chip_info->dbr, \
  876. + spi->max_speed_hz);
  877. +
  878. + chip->ctar.cpha = (spi->mode & SPI_CPHA) ? 1 : 0;
  879. + chip->ctar.cpol = (spi->mode & SPI_CPOL) ? 1 : 0;
  880. + chip->ctar.lsbfe = (spi->mode & SPI_LSB_FIRST) ? 1 : 0;
  881. + chip->ctar.dbr = chip_info->dbr;
  882. + chip->ctar.pbr = chip_info->pbr;
  883. + chip->ctar.br = chip_info->br;
  884. + chip->ctar.pcssck = chip_info->pcssck;
  885. + chip->ctar.pasc = chip_info->pasc;
  886. + chip->ctar.pdt = chip_info->pdt;
  887. + chip->ctar.cssck = chip_info->cssck;
  888. + chip->ctar.asc = chip_info->asc;
  889. + chip->ctar.dt = chip_info->dt;
  890. +
  891. + spi_set_ctldata(spi, chip);
  892. +
  893. + return 0;
  894. +}
  895. +
  896. +static int init_queue(struct driver_data *drv_data)
  897. +{
  898. + INIT_LIST_HEAD(&drv_data->queue);
  899. + spin_lock_init(&drv_data->lock);
  900. +
  901. + drv_data->run = QUEUE_STOPPED;
  902. + drv_data->busy = 0;
  903. +
  904. + tasklet_init(&drv_data->pump_transfers,
  905. + pump_transfers, (unsigned long)drv_data);
  906. +
  907. + INIT_WORK(&drv_data->pump_messages, pump_messages);
  908. +
  909. + drv_data->workqueue = create_singlethread_workqueue(
  910. + drv_data->master->dev.parent->bus_id);
  911. + if (drv_data->workqueue == NULL)
  912. + return -EBUSY;
  913. +
  914. + return 0;
  915. +}
  916. +
  917. +static int start_queue(struct driver_data *drv_data)
  918. +{
  919. + unsigned long flags;
  920. +
  921. + spin_lock_irqsave(&drv_data->lock, flags);
  922. +
  923. + if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
  924. + spin_unlock_irqrestore(&drv_data->lock, flags);
  925. + return -EBUSY;
  926. + }
  927. +
  928. + drv_data->run = QUEUE_RUNNING;
  929. + drv_data->cur_msg = NULL;
  930. + drv_data->cur_transfer = NULL;
  931. + drv_data->cur_chip = NULL;
  932. + spin_unlock_irqrestore(&drv_data->lock, flags);
  933. +
  934. + queue_work(drv_data->workqueue, &drv_data->pump_messages);
  935. +
  936. + return 0;
  937. +}
  938. +
  939. +static int stop_queue(struct driver_data *drv_data)
  940. +{
  941. + unsigned long flags;
  942. + unsigned limit = 500;
  943. + int status = 0;
  944. +
  945. + spin_lock_irqsave(&drv_data->lock, flags);
  946. +
  947. + /* This is a bit lame, but is optimized for the common execution path.
  948. + * A wait_queue on the drv_data->busy could be used, but then the common
  949. + * execution path (pump_messages) would be required to call wake_up or
  950. + * friends on every SPI message. Do this instead */
  951. + drv_data->run = QUEUE_STOPPED;
  952. + while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
  953. + spin_unlock_irqrestore(&drv_data->lock, flags);
  954. + msleep(10);
  955. + spin_lock_irqsave(&drv_data->lock, flags);
  956. + }
  957. +
  958. + if (!list_empty(&drv_data->queue) || drv_data->busy)
  959. + status = -EBUSY;
  960. +
  961. + spin_unlock_irqrestore(&drv_data->lock, flags);
  962. +
  963. + return status;
  964. +}
  965. +
  966. +static int destroy_queue(struct driver_data *drv_data)
  967. +{
  968. + int status;
  969. +
  970. + status = stop_queue(drv_data);
  971. + if (status != 0)
  972. + return status;
  973. +
  974. + destroy_workqueue(drv_data->workqueue);
  975. +
  976. + return 0;
  977. +}
  978. +
  979. +
  980. +static void cleanup(struct spi_device *spi)
  981. +{
  982. + struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
  983. +
  984. + dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
  985. + spi->master->bus_num, spi->chip_select);
  986. +
  987. + kfree(chip);
  988. +}
  989. +
  990. +
  991. +/****************************************************************************/
  992. +
  993. +/*
  994. + * Generic Device driver routines and interface implementation
  995. + */
  996. +
  997. +static int coldfire_spi_probe(struct platform_device *pdev)
  998. +{
  999. + struct device *dev = &pdev->dev;
  1000. + struct coldfire_spi_master *platform_info;
  1001. + struct spi_master *master;
  1002. + struct driver_data *drv_data = 0;
  1003. + struct resource *memory_resource;
  1004. + int irq;
  1005. + int status = 0;
  1006. + int i;
  1007. +#if defined(SPI_DSPI_EDMA)
  1008. + dma_addr_t dma_handle;
  1009. +#endif
  1010. +
  1011. + platform_info = (struct coldfire_spi_master *)dev->platform_data;
  1012. +
  1013. + master = spi_alloc_master(dev, sizeof(struct driver_data));
  1014. + if (!master)
  1015. + return -ENOMEM;
  1016. +
  1017. + drv_data = spi_master_get_devdata(master);
  1018. + drv_data->master = master;
  1019. +
  1020. + INIT_LIST_HEAD(&drv_data->queue);
  1021. + spin_lock_init(&drv_data->lock);
  1022. +
  1023. + master->bus_num = platform_info->bus_num;
  1024. + master->num_chipselect = platform_info->num_chipselect;
  1025. + master->cleanup = cleanup;
  1026. + master->setup = setup;
  1027. + master->transfer = transfer;
  1028. +
  1029. + drv_data->cs_control = platform_info->cs_control;
  1030. + if (drv_data->cs_control)
  1031. + for (i = 0; i < master->num_chipselect; i++)
  1032. + drv_data->cs_control(i, QSPI_CS_INIT | QSPI_CS_DROP);
  1033. +
  1034. + /* Setup register addresses */
  1035. + memory_resource = platform_get_resource_byname(pdev,
  1036. + IORESOURCE_MEM, "spi-module");
  1037. + if (!memory_resource) {
  1038. + dev_dbg(dev, "can not find platform module memory\n");
  1039. + goto out_error_master_alloc;
  1040. + }
  1041. +
  1042. +#if defined(SPI_DSPI_EDMA)
  1043. + drv_data->edma_tx_buf = dma_alloc_coherent(NULL, EDMA_BUFSIZE_KMALLOC, \
  1044. + &dma_handle, GFP_DMA);
  1045. + if (!drv_data->edma_tx_buf) {
  1046. + dev_dbg(dev, "cannot allocate eDMA TX memory\n");
  1047. + goto out_error_master_alloc;
  1048. + }
  1049. + drv_data->edma_rx_buf = dma_alloc_coherent(NULL, EDMA_BUFSIZE_KMALLOC, \
  1050. + &dma_handle, GFP_DMA);
  1051. + if (!drv_data->edma_rx_buf) {
  1052. + dma_free_coherent(NULL, EDMA_BUFSIZE_KMALLOC, \
  1053. + drv_data->edma_tx_buf, dma_handle);
  1054. + kfree(drv_data->edma_tx_buf);
  1055. + dev_dbg(dev, "cannot allocate eDMA RX memory\n");
  1056. + goto out_error_master_alloc;
  1057. + }
  1058. +#endif
  1059. +
  1060. + drv_data->mcr = (void *)&MCF_DSPI_DMCR;
  1061. + drv_data->ctar = (void *)&MCF_DSPI_DCTAR0;
  1062. + drv_data->dspi_sr = (void *)&MCF_DSPI_DSR;
  1063. + drv_data->dspi_rser = (void *)&MCF_DSPI_DRSER;
  1064. + drv_data->dspi_dtfr = (void *)&MCF_DSPI_DTFR;
  1065. + drv_data->dspi_drfr = (void *)&MCF_DSPI_DRFR;
  1066. +
  1067. + memory_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1068. + "spi-par");
  1069. + if (!memory_resource) {
  1070. + dev_dbg(dev, "No spi-par memory\n");
  1071. + goto out_error_master_alloc;
  1072. + }
  1073. + drv_data->par = (void *)memory_resource->start;
  1074. +
  1075. + memory_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1076. + "spi-int-level");
  1077. + if (!memory_resource) {
  1078. + dev_dbg(dev, "No spi-int-level memory\n");
  1079. + goto out_error_master_alloc;
  1080. + }
  1081. + drv_data->int_icr = (void *)memory_resource->start;
  1082. +
  1083. + memory_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1084. + "spi-int-mask");
  1085. + if (!memory_resource) {
  1086. + dev_dbg(dev, "No spi-int-mask memory\n");
  1087. + goto out_error_master_alloc;
  1088. + }
  1089. + drv_data->int_mr = (void *)memory_resource->start;
  1090. +
  1091. + if (platform_info->irq_list) {
  1092. + /* multiple IRQs */
  1093. + int *irqlist = platform_info->irq_list;
  1094. + while ((irq = *irqlist++)) {
  1095. + int off = *irqlist++;
  1096. + int lvl = *irqlist++;
  1097. + int msk = *irqlist++;
  1098. + status = request_irq(irq, dspi_interrupt, IRQF_DISABLED,
  1099. + dev->bus_id, drv_data);
  1100. + if (status < 0) {
  1101. + dev_err(&pdev->dev,
  1102. + "Unable to attach ColdFire DSPI interrupt\n");
  1103. + goto out_error_master_alloc;
  1104. + }
  1105. +
  1106. + if (lvl)
  1107. + *(drv_data->int_icr + off) = lvl;
  1108. +
  1109. + if (msk)
  1110. + *drv_data->int_mr &= ~msk;
  1111. + }
  1112. + }
  1113. + else {
  1114. + irq = platform_info->irq_vector;
  1115. +
  1116. + status = request_irq(platform_info->irq_vector, dspi_interrupt,
  1117. + IRQF_DISABLED, dev->bus_id, drv_data);
  1118. + if (status < 0) {
  1119. + dev_err(&pdev->dev, "Unable to attach ColdFire DSPI interrupt\n");
  1120. + goto out_error_master_alloc;
  1121. + }
  1122. +
  1123. + *drv_data->int_icr = platform_info->irq_lp;
  1124. + *drv_data->int_mr &= ~platform_info->irq_mask;
  1125. + }
  1126. +
  1127. + /* Now that we have all the addresses etc. Let's set it up */
  1128. + if (platform_info->par_val)
  1129. + *drv_data->par = platform_info->par_val;
  1130. +
  1131. + /* Initial and start queue */
  1132. + status = init_queue(drv_data);
  1133. + if (status != 0) {
  1134. + dev_err(&pdev->dev, "Problem initializing DSPI queue\n");
  1135. + goto out_error_irq_alloc;
  1136. + }
  1137. + status = start_queue(drv_data);
  1138. + if (status != 0) {
  1139. + dev_err(&pdev->dev, "Problem starting DSPI queue\n");
  1140. + goto out_error_irq_alloc;
  1141. + }
  1142. +
  1143. + /* Register with the SPI framework */
  1144. + platform_set_drvdata(pdev, drv_data);
  1145. + status = spi_register_master(master);
  1146. + if (status != 0) {
  1147. + dev_err(&pdev->dev, "Problem registering DSPI master\n");
  1148. + status = -EINVAL;
  1149. + goto out_error_queue_alloc;
  1150. + }
  1151. +
  1152. +#if defined(SPI_DSPI_EDMA)
  1153. + if (mcf_edma_request_channel(DSPI_DMA_TX_TCD,
  1154. + edma_tx_handler,
  1155. + NULL,
  1156. + 0x0,
  1157. + pdev,
  1158. + NULL, /* spinlock */
  1159. + DRIVER_NAME) < 0){
  1160. + dev_err(&pdev->dev, "eDMA transmit channel request\n");
  1161. + status = -EINVAL;
  1162. + goto out_error_queue_alloc;
  1163. + }
  1164. +
  1165. + if (mcf_edma_request_channel(DSPI_DMA_RX_TCD,
  1166. + edma_rx_handler,
  1167. + NULL,
  1168. + 0x0,
  1169. + pdev,
  1170. + NULL, /* spinlock */
  1171. + DRIVER_NAME) < 0){
  1172. + dev_err(&pdev->dev, "eDAM receive channel request\n");
  1173. + status = -EINVAL;
  1174. + mcf_edma_free_channel(DSPI_DMA_TX_TCD, pdev);
  1175. + goto out_error_queue_alloc;
  1176. + }
  1177. +#endif
  1178. +
  1179. + printk(KERN_INFO "DSPI: Coldfire master initialized\n");
  1180. + return status;
  1181. +
  1182. +out_error_queue_alloc:
  1183. + destroy_queue(drv_data);
  1184. +
  1185. +out_error_irq_alloc:
  1186. + free_irq(platform_info->irq_vector, drv_data);
  1187. +
  1188. +out_error_master_alloc:
  1189. + spi_master_put(master);
  1190. + return status;
  1191. +
  1192. +}
  1193. +
  1194. +static int coldfire_spi_remove(struct platform_device *pdev)
  1195. +{
  1196. + struct driver_data *drv_data = platform_get_drvdata(pdev);
  1197. + int irq;
  1198. + int status = 0;
  1199. +
  1200. + if (!drv_data)
  1201. + return 0;
  1202. +
  1203. +#if defined(SPI_DSPI_EDMA)
  1204. + mcf_edma_free_channel(DSPI_DMA_TX_TCD, pdev);
  1205. + mcf_edma_free_channel(DSPI_DMA_RX_TCD, pdev);
  1206. +#endif
  1207. +
  1208. + /* Remove the queue */
  1209. + status = destroy_queue(drv_data);
  1210. + if (status != 0)
  1211. + return status;
  1212. +
  1213. + /* Release IRQ */
  1214. + irq = platform_get_irq(pdev, 0);
  1215. + if (irq >= 0)
  1216. + free_irq(irq, drv_data);
  1217. +
  1218. + /* Disconnect from the SPI framework */
  1219. + spi_unregister_master(drv_data->master);
  1220. +
  1221. + /* Prevent double remove */
  1222. + platform_set_drvdata(pdev, NULL);
  1223. +
  1224. + return 0;
  1225. +}
  1226. +
  1227. +static void coldfire_spi_shutdown(struct platform_device *pdev)
  1228. +{
  1229. + int status = coldfire_spi_remove(pdev);
  1230. +
  1231. + if (status != 0)
  1232. + dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1233. +}
  1234. +
  1235. +
  1236. +#ifdef CONFIG_PM
  1237. +static int suspend_devices(struct device *dev, void *pm_message)
  1238. +{
  1239. + pm_message_t *state = pm_message;
  1240. +
  1241. + if (dev->power.power_state.event != state->event) {
  1242. + dev_warn(dev, "pm state does not match request\n");
  1243. + return -1;
  1244. + }
  1245. +
  1246. + return 0;
  1247. +}
  1248. +
  1249. +static int coldfire_spi_suspend(struct platform_device *pdev,
  1250. + pm_message_t state)
  1251. +{
  1252. + struct driver_data *drv_data = platform_get_drvdata(pdev);
  1253. + int status = 0;
  1254. +
  1255. + /* Check all childern for current power state */
  1256. + if (device_for_each_child(&pdev->dev,
  1257. + &state, suspend_devices) != 0) {
  1258. + dev_warn(&pdev->dev, "suspend aborted\n");
  1259. + return -1;
  1260. + }
  1261. +
  1262. + status = stop_queue(drv_data);
  1263. + if (status != 0)
  1264. + return status;
  1265. +
  1266. + return 0;
  1267. +}
  1268. +
  1269. +static int coldfire_spi_resume(struct platform_device *pdev)
  1270. +{
  1271. + struct driver_data *drv_data = platform_get_drvdata(pdev);
  1272. + int status = 0;
  1273. +
  1274. + /* Start the queue running */
  1275. + status = start_queue(drv_data);
  1276. + if (status != 0) {
  1277. + dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
  1278. + return status;
  1279. + }
  1280. +
  1281. + return 0;
  1282. +}
  1283. +#else
  1284. +#define coldfire_spi_suspend NULL
  1285. +#define coldfire_spi_resume NULL
  1286. +#endif /* CONFIG_PM */
  1287. +
  1288. +static struct platform_driver driver = {
  1289. + .driver = {
  1290. + .name = "spi_coldfire",
  1291. + .bus = &platform_bus_type,
  1292. + .owner = THIS_MODULE,
  1293. + },
  1294. + .probe = coldfire_spi_probe,
  1295. + .remove = __devexit_p(coldfire_spi_remove),
  1296. + .shutdown = coldfire_spi_shutdown,
  1297. + .suspend = coldfire_spi_suspend,
  1298. + .resume = coldfire_spi_resume,
  1299. +};
  1300. +
  1301. +static int __init coldfire_spi_init(void)
  1302. +{
  1303. + platform_driver_register(&driver);
  1304. +
  1305. + return 0;
  1306. +}
  1307. +module_init(coldfire_spi_init);
  1308. +
  1309. +static void __exit coldfire_spi_exit(void)
  1310. +{
  1311. + platform_driver_unregister(&driver);
  1312. +}
  1313. +module_exit(coldfire_spi_exit);
  1314. +
  1315. +MODULE_AUTHOR("Matt Waddel");
  1316. +MODULE_DESCRIPTION("ColdFire DSPI Contoller");
  1317. +MODULE_LICENSE("GPL");
  1318. --- /dev/null
  1319. +++ b/include/asm-m68k/mcfdspi.h
  1320. @@ -0,0 +1,48 @@
  1321. +/*
  1322. + * mcfdspi.h-DSPI controller for the ColdFire processors.
  1323. + *
  1324. + * Andrey Butok
  1325. + * Copyright Freescale Semiconductor, Inc. 2008
  1326. + *
  1327. + * This file is based on mcfqspi.h
  1328. + *
  1329. + * This program is free software; you can redistribute it and/or modify it
  1330. + * under the terms of the GNU General Public License as published by the
  1331. + * Free Software Foundation; either version 2 of the License, or (at your
  1332. + * option) any later version.
  1333. + *
  1334. + * This program is distributed in the hope that it will be useful,
  1335. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1336. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1337. + * GNU General Public License for more details.
  1338. + *
  1339. + * You should have received a copy of the GNU General Public License
  1340. + * along with this program; if not, write to the Free Software
  1341. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  1342. + *
  1343. + ***************************************************************************
  1344. + * Changes:
  1345. + * v0.001 25 March 2008 Andrey Butok
  1346. + * Initial Release - developed on uClinux with 2.6.23 kernel.
  1347. + *
  1348. + */
  1349. +
  1350. +#ifndef MCFDSPI_H_
  1351. +#define MCFDSPI_H_
  1352. +
  1353. +struct coldfire_dspi_chip {
  1354. + u8 mode;
  1355. + u8 bits_per_word;
  1356. + u16 void_write_data;
  1357. + /* Only used in master mode */
  1358. + u8 dbr; /* Double baud rate */
  1359. + u8 pbr; /* Baud rate prescaler */
  1360. + u8 br; /* Baud rate scaler */
  1361. + u8 pcssck; /* PCS to SCK delay prescaler */
  1362. + u8 pasc; /* After SCK delay prescaler */
  1363. + u8 pdt; /* Delay after transfer prescaler */
  1364. + u8 cssck; /* PCS to SCK delay scaler */
  1365. + u8 asc; /* After SCK delay scaler */
  1366. + u8 dt; /* Delay after transfer scaler */
  1367. +};
  1368. +#endif /*MCFDSPI_H_*/