amba-pl022.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305
  1. /*
  2. * drivers/spi/amba-pl022.c
  3. *
  4. * A driver for the ARM PL022 PrimeCell SSP/SPI bus master.
  5. *
  6. * Copyright (C) 2008-2009 ST-Ericsson AB
  7. * Copyright (C) 2006 STMicroelectronics Pvt. Ltd.
  8. *
  9. * Author: Linus Walleij <linus.walleij@stericsson.com>
  10. *
  11. * Initial version inspired by:
  12. * linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c
  13. * Initial adoption to PL022 by:
  14. * Sachin Verma <sachin.verma@st.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. */
  26. /*
  27. * TODO:
  28. * - add timeout on polled transfers
  29. */
  30. #include <linux/init.h>
  31. #include <linux/module.h>
  32. #include <linux/device.h>
  33. #include <linux/ioport.h>
  34. #include <linux/errno.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/spi/spi.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/delay.h>
  39. #include <linux/clk.h>
  40. #include <linux/err.h>
  41. #include <linux/amba/bus.h>
  42. #include <linux/amba/pl022.h>
  43. #include <linux/io.h>
  44. #include <linux/slab.h>
  45. #include <linux/dmaengine.h>
  46. #include <linux/dma-mapping.h>
  47. #include <linux/scatterlist.h>
  48. /*
  49. * This macro is used to define some register default values.
  50. * reg is masked with mask, the OR:ed with an (again masked)
  51. * val shifted sb steps to the left.
  52. */
  53. #define SSP_WRITE_BITS(reg, val, mask, sb) \
  54. ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
  55. /*
  56. * This macro is also used to define some default values.
  57. * It will just shift val by sb steps to the left and mask
  58. * the result with mask.
  59. */
  60. #define GEN_MASK_BITS(val, mask, sb) \
  61. (((val)<<(sb)) & (mask))
  62. #define DRIVE_TX 0
  63. #define DO_NOT_DRIVE_TX 1
  64. #define DO_NOT_QUEUE_DMA 0
  65. #define QUEUE_DMA 1
  66. #define RX_TRANSFER 1
  67. #define TX_TRANSFER 2
  68. /*
  69. * Macros to access SSP Registers with their offsets
  70. */
  71. #define SSP_CR0(r) (r + 0x000)
  72. #define SSP_CR1(r) (r + 0x004)
  73. #define SSP_DR(r) (r + 0x008)
  74. #define SSP_SR(r) (r + 0x00C)
  75. #define SSP_CPSR(r) (r + 0x010)
  76. #define SSP_IMSC(r) (r + 0x014)
  77. #define SSP_RIS(r) (r + 0x018)
  78. #define SSP_MIS(r) (r + 0x01C)
  79. #define SSP_ICR(r) (r + 0x020)
  80. #define SSP_DMACR(r) (r + 0x024)
  81. #define SSP_ITCR(r) (r + 0x080)
  82. #define SSP_ITIP(r) (r + 0x084)
  83. #define SSP_ITOP(r) (r + 0x088)
  84. #define SSP_TDR(r) (r + 0x08C)
  85. #define SSP_PID0(r) (r + 0xFE0)
  86. #define SSP_PID1(r) (r + 0xFE4)
  87. #define SSP_PID2(r) (r + 0xFE8)
  88. #define SSP_PID3(r) (r + 0xFEC)
  89. #define SSP_CID0(r) (r + 0xFF0)
  90. #define SSP_CID1(r) (r + 0xFF4)
  91. #define SSP_CID2(r) (r + 0xFF8)
  92. #define SSP_CID3(r) (r + 0xFFC)
  93. /*
  94. * SSP Control Register 0 - SSP_CR0
  95. */
  96. #define SSP_CR0_MASK_DSS (0x0FUL << 0)
  97. #define SSP_CR0_MASK_FRF (0x3UL << 4)
  98. #define SSP_CR0_MASK_SPO (0x1UL << 6)
  99. #define SSP_CR0_MASK_SPH (0x1UL << 7)
  100. #define SSP_CR0_MASK_SCR (0xFFUL << 8)
  101. /*
  102. * The ST version of this block moves som bits
  103. * in SSP_CR0 and extends it to 32 bits
  104. */
  105. #define SSP_CR0_MASK_DSS_ST (0x1FUL << 0)
  106. #define SSP_CR0_MASK_HALFDUP_ST (0x1UL << 5)
  107. #define SSP_CR0_MASK_CSS_ST (0x1FUL << 16)
  108. #define SSP_CR0_MASK_FRF_ST (0x3UL << 21)
  109. /*
  110. * SSP Control Register 0 - SSP_CR1
  111. */
  112. #define SSP_CR1_MASK_LBM (0x1UL << 0)
  113. #define SSP_CR1_MASK_SSE (0x1UL << 1)
  114. #define SSP_CR1_MASK_MS (0x1UL << 2)
  115. #define SSP_CR1_MASK_SOD (0x1UL << 3)
  116. /*
  117. * The ST version of this block adds some bits
  118. * in SSP_CR1
  119. */
  120. #define SSP_CR1_MASK_RENDN_ST (0x1UL << 4)
  121. #define SSP_CR1_MASK_TENDN_ST (0x1UL << 5)
  122. #define SSP_CR1_MASK_MWAIT_ST (0x1UL << 6)
  123. #define SSP_CR1_MASK_RXIFLSEL_ST (0x7UL << 7)
  124. #define SSP_CR1_MASK_TXIFLSEL_ST (0x7UL << 10)
  125. /* This one is only in the PL023 variant */
  126. #define SSP_CR1_MASK_FBCLKDEL_ST (0x7UL << 13)
  127. /*
  128. * SSP Status Register - SSP_SR
  129. */
  130. #define SSP_SR_MASK_TFE (0x1UL << 0) /* Transmit FIFO empty */
  131. #define SSP_SR_MASK_TNF (0x1UL << 1) /* Transmit FIFO not full */
  132. #define SSP_SR_MASK_RNE (0x1UL << 2) /* Receive FIFO not empty */
  133. #define SSP_SR_MASK_RFF (0x1UL << 3) /* Receive FIFO full */
  134. #define SSP_SR_MASK_BSY (0x1UL << 4) /* Busy Flag */
  135. /*
  136. * SSP Clock Prescale Register - SSP_CPSR
  137. */
  138. #define SSP_CPSR_MASK_CPSDVSR (0xFFUL << 0)
  139. /*
  140. * SSP Interrupt Mask Set/Clear Register - SSP_IMSC
  141. */
  142. #define SSP_IMSC_MASK_RORIM (0x1UL << 0) /* Receive Overrun Interrupt mask */
  143. #define SSP_IMSC_MASK_RTIM (0x1UL << 1) /* Receive timeout Interrupt mask */
  144. #define SSP_IMSC_MASK_RXIM (0x1UL << 2) /* Receive FIFO Interrupt mask */
  145. #define SSP_IMSC_MASK_TXIM (0x1UL << 3) /* Transmit FIFO Interrupt mask */
  146. /*
  147. * SSP Raw Interrupt Status Register - SSP_RIS
  148. */
  149. /* Receive Overrun Raw Interrupt status */
  150. #define SSP_RIS_MASK_RORRIS (0x1UL << 0)
  151. /* Receive Timeout Raw Interrupt status */
  152. #define SSP_RIS_MASK_RTRIS (0x1UL << 1)
  153. /* Receive FIFO Raw Interrupt status */
  154. #define SSP_RIS_MASK_RXRIS (0x1UL << 2)
  155. /* Transmit FIFO Raw Interrupt status */
  156. #define SSP_RIS_MASK_TXRIS (0x1UL << 3)
  157. /*
  158. * SSP Masked Interrupt Status Register - SSP_MIS
  159. */
  160. /* Receive Overrun Masked Interrupt status */
  161. #define SSP_MIS_MASK_RORMIS (0x1UL << 0)
  162. /* Receive Timeout Masked Interrupt status */
  163. #define SSP_MIS_MASK_RTMIS (0x1UL << 1)
  164. /* Receive FIFO Masked Interrupt status */
  165. #define SSP_MIS_MASK_RXMIS (0x1UL << 2)
  166. /* Transmit FIFO Masked Interrupt status */
  167. #define SSP_MIS_MASK_TXMIS (0x1UL << 3)
  168. /*
  169. * SSP Interrupt Clear Register - SSP_ICR
  170. */
  171. /* Receive Overrun Raw Clear Interrupt bit */
  172. #define SSP_ICR_MASK_RORIC (0x1UL << 0)
  173. /* Receive Timeout Clear Interrupt bit */
  174. #define SSP_ICR_MASK_RTIC (0x1UL << 1)
  175. /*
  176. * SSP DMA Control Register - SSP_DMACR
  177. */
  178. /* Receive DMA Enable bit */
  179. #define SSP_DMACR_MASK_RXDMAE (0x1UL << 0)
  180. /* Transmit DMA Enable bit */
  181. #define SSP_DMACR_MASK_TXDMAE (0x1UL << 1)
  182. /*
  183. * SSP Integration Test control Register - SSP_ITCR
  184. */
  185. #define SSP_ITCR_MASK_ITEN (0x1UL << 0)
  186. #define SSP_ITCR_MASK_TESTFIFO (0x1UL << 1)
  187. /*
  188. * SSP Integration Test Input Register - SSP_ITIP
  189. */
  190. #define ITIP_MASK_SSPRXD (0x1UL << 0)
  191. #define ITIP_MASK_SSPFSSIN (0x1UL << 1)
  192. #define ITIP_MASK_SSPCLKIN (0x1UL << 2)
  193. #define ITIP_MASK_RXDMAC (0x1UL << 3)
  194. #define ITIP_MASK_TXDMAC (0x1UL << 4)
  195. #define ITIP_MASK_SSPTXDIN (0x1UL << 5)
  196. /*
  197. * SSP Integration Test output Register - SSP_ITOP
  198. */
  199. #define ITOP_MASK_SSPTXD (0x1UL << 0)
  200. #define ITOP_MASK_SSPFSSOUT (0x1UL << 1)
  201. #define ITOP_MASK_SSPCLKOUT (0x1UL << 2)
  202. #define ITOP_MASK_SSPOEn (0x1UL << 3)
  203. #define ITOP_MASK_SSPCTLOEn (0x1UL << 4)
  204. #define ITOP_MASK_RORINTR (0x1UL << 5)
  205. #define ITOP_MASK_RTINTR (0x1UL << 6)
  206. #define ITOP_MASK_RXINTR (0x1UL << 7)
  207. #define ITOP_MASK_TXINTR (0x1UL << 8)
  208. #define ITOP_MASK_INTR (0x1UL << 9)
  209. #define ITOP_MASK_RXDMABREQ (0x1UL << 10)
  210. #define ITOP_MASK_RXDMASREQ (0x1UL << 11)
  211. #define ITOP_MASK_TXDMABREQ (0x1UL << 12)
  212. #define ITOP_MASK_TXDMASREQ (0x1UL << 13)
  213. /*
  214. * SSP Test Data Register - SSP_TDR
  215. */
  216. #define TDR_MASK_TESTDATA (0xFFFFFFFF)
  217. /*
  218. * Message State
  219. * we use the spi_message.state (void *) pointer to
  220. * hold a single state value, that's why all this
  221. * (void *) casting is done here.
  222. */
  223. #define STATE_START ((void *) 0)
  224. #define STATE_RUNNING ((void *) 1)
  225. #define STATE_DONE ((void *) 2)
  226. #define STATE_ERROR ((void *) -1)
  227. /*
  228. * SSP State - Whether Enabled or Disabled
  229. */
  230. #define SSP_DISABLED (0)
  231. #define SSP_ENABLED (1)
  232. /*
  233. * SSP DMA State - Whether DMA Enabled or Disabled
  234. */
  235. #define SSP_DMA_DISABLED (0)
  236. #define SSP_DMA_ENABLED (1)
  237. /*
  238. * SSP Clock Defaults
  239. */
  240. #define SSP_DEFAULT_CLKRATE 0x2
  241. #define SSP_DEFAULT_PRESCALE 0x40
  242. /*
  243. * SSP Clock Parameter ranges
  244. */
  245. #define CPSDVR_MIN 0x02
  246. #define CPSDVR_MAX 0xFE
  247. #define SCR_MIN 0x00
  248. #define SCR_MAX 0xFF
  249. /*
  250. * SSP Interrupt related Macros
  251. */
  252. #define DEFAULT_SSP_REG_IMSC 0x0UL
  253. #define DISABLE_ALL_INTERRUPTS DEFAULT_SSP_REG_IMSC
  254. #define ENABLE_ALL_INTERRUPTS (~DEFAULT_SSP_REG_IMSC)
  255. #define CLEAR_ALL_INTERRUPTS 0x3
  256. /*
  257. * The type of reading going on on this chip
  258. */
  259. enum ssp_reading {
  260. READING_NULL,
  261. READING_U8,
  262. READING_U16,
  263. READING_U32
  264. };
  265. /**
  266. * The type of writing going on on this chip
  267. */
  268. enum ssp_writing {
  269. WRITING_NULL,
  270. WRITING_U8,
  271. WRITING_U16,
  272. WRITING_U32
  273. };
  274. /**
  275. * struct vendor_data - vendor-specific config parameters
  276. * for PL022 derivates
  277. * @fifodepth: depth of FIFOs (both)
  278. * @max_bpw: maximum number of bits per word
  279. * @unidir: supports unidirection transfers
  280. * @extended_cr: 32 bit wide control register 0 with extra
  281. * features and extra features in CR1 as found in the ST variants
  282. * @pl023: supports a subset of the ST extensions called "PL023"
  283. */
  284. struct vendor_data {
  285. int fifodepth;
  286. int max_bpw;
  287. bool unidir;
  288. bool extended_cr;
  289. bool pl023;
  290. };
  291. /**
  292. * struct pl022 - This is the private SSP driver data structure
  293. * @adev: AMBA device model hookup
  294. * @vendor: Vendor data for the IP block
  295. * @phybase: The physical memory where the SSP device resides
  296. * @virtbase: The virtual memory where the SSP is mapped
  297. * @master: SPI framework hookup
  298. * @master_info: controller-specific data from machine setup
  299. * @regs: SSP controller register's virtual address
  300. * @pump_messages: Work struct for scheduling work to the workqueue
  301. * @lock: spinlock to syncronise access to driver data
  302. * @workqueue: a workqueue on which any spi_message request is queued
  303. * @busy: workqueue is busy
  304. * @running: workqueue is running
  305. * @pump_transfers: Tasklet used in Interrupt Transfer mode
  306. * @cur_msg: Pointer to current spi_message being processed
  307. * @cur_transfer: Pointer to current spi_transfer
  308. * @cur_chip: pointer to current clients chip(assigned from controller_state)
  309. * @tx: current position in TX buffer to be read
  310. * @tx_end: end position in TX buffer to be read
  311. * @rx: current position in RX buffer to be written
  312. * @rx_end: end position in RX buffer to be written
  313. * @readingtype: the type of read currently going on
  314. * @writingtype: the type or write currently going on
  315. */
  316. struct pl022 {
  317. struct amba_device *adev;
  318. struct vendor_data *vendor;
  319. resource_size_t phybase;
  320. void __iomem *virtbase;
  321. struct clk *clk;
  322. struct spi_master *master;
  323. struct pl022_ssp_controller *master_info;
  324. /* Driver message queue */
  325. struct workqueue_struct *workqueue;
  326. struct work_struct pump_messages;
  327. spinlock_t queue_lock;
  328. struct list_head queue;
  329. bool busy;
  330. bool running;
  331. /* Message transfer pump */
  332. struct tasklet_struct pump_transfers;
  333. struct spi_message *cur_msg;
  334. struct spi_transfer *cur_transfer;
  335. struct chip_data *cur_chip;
  336. void *tx;
  337. void *tx_end;
  338. void *rx;
  339. void *rx_end;
  340. enum ssp_reading read;
  341. enum ssp_writing write;
  342. u32 exp_fifo_level;
  343. /* DMA settings */
  344. #ifdef CONFIG_DMA_ENGINE
  345. struct dma_chan *dma_rx_channel;
  346. struct dma_chan *dma_tx_channel;
  347. struct sg_table sgt_rx;
  348. struct sg_table sgt_tx;
  349. char *dummypage;
  350. #endif
  351. };
  352. /**
  353. * struct chip_data - To maintain runtime state of SSP for each client chip
  354. * @cr0: Value of control register CR0 of SSP - on later ST variants this
  355. * register is 32 bits wide rather than just 16
  356. * @cr1: Value of control register CR1 of SSP
  357. * @dmacr: Value of DMA control Register of SSP
  358. * @cpsr: Value of Clock prescale register
  359. * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
  360. * @enable_dma: Whether to enable DMA or not
  361. * @write: function ptr to be used to write when doing xfer for this chip
  362. * @read: function ptr to be used to read when doing xfer for this chip
  363. * @cs_control: chip select callback provided by chip
  364. * @xfer_type: polling/interrupt/DMA
  365. *
  366. * Runtime state of the SSP controller, maintained per chip,
  367. * This would be set according to the current message that would be served
  368. */
  369. struct chip_data {
  370. u32 cr0;
  371. u16 cr1;
  372. u16 dmacr;
  373. u16 cpsr;
  374. u8 n_bytes;
  375. bool enable_dma;
  376. enum ssp_reading read;
  377. enum ssp_writing write;
  378. void (*cs_control) (u32 command);
  379. int xfer_type;
  380. };
  381. /**
  382. * null_cs_control - Dummy chip select function
  383. * @command: select/delect the chip
  384. *
  385. * If no chip select function is provided by client this is used as dummy
  386. * chip select
  387. */
  388. static void null_cs_control(u32 command)
  389. {
  390. pr_debug("pl022: dummy chip select control, CS=0x%x\n", command);
  391. }
  392. /**
  393. * giveback - current spi_message is over, schedule next message and call
  394. * callback of this message. Assumes that caller already
  395. * set message->status; dma and pio irqs are blocked
  396. * @pl022: SSP driver private data structure
  397. */
  398. static void giveback(struct pl022 *pl022)
  399. {
  400. struct spi_transfer *last_transfer;
  401. unsigned long flags;
  402. struct spi_message *msg;
  403. void (*curr_cs_control) (u32 command);
  404. /*
  405. * This local reference to the chip select function
  406. * is needed because we set curr_chip to NULL
  407. * as a step toward termininating the message.
  408. */
  409. curr_cs_control = pl022->cur_chip->cs_control;
  410. spin_lock_irqsave(&pl022->queue_lock, flags);
  411. msg = pl022->cur_msg;
  412. pl022->cur_msg = NULL;
  413. pl022->cur_transfer = NULL;
  414. pl022->cur_chip = NULL;
  415. queue_work(pl022->workqueue, &pl022->pump_messages);
  416. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  417. last_transfer = list_entry(msg->transfers.prev,
  418. struct spi_transfer,
  419. transfer_list);
  420. /* Delay if requested before any change in chip select */
  421. if (last_transfer->delay_usecs)
  422. /*
  423. * FIXME: This runs in interrupt context.
  424. * Is this really smart?
  425. */
  426. udelay(last_transfer->delay_usecs);
  427. /*
  428. * Drop chip select UNLESS cs_change is true or we are returning
  429. * a message with an error, or next message is for another chip
  430. */
  431. if (!last_transfer->cs_change)
  432. curr_cs_control(SSP_CHIP_DESELECT);
  433. else {
  434. struct spi_message *next_msg;
  435. /* Holding of cs was hinted, but we need to make sure
  436. * the next message is for the same chip. Don't waste
  437. * time with the following tests unless this was hinted.
  438. *
  439. * We cannot postpone this until pump_messages, because
  440. * after calling msg->complete (below) the driver that
  441. * sent the current message could be unloaded, which
  442. * could invalidate the cs_control() callback...
  443. */
  444. /* get a pointer to the next message, if any */
  445. spin_lock_irqsave(&pl022->queue_lock, flags);
  446. if (list_empty(&pl022->queue))
  447. next_msg = NULL;
  448. else
  449. next_msg = list_entry(pl022->queue.next,
  450. struct spi_message, queue);
  451. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  452. /* see if the next and current messages point
  453. * to the same chip
  454. */
  455. if (next_msg && next_msg->spi != msg->spi)
  456. next_msg = NULL;
  457. if (!next_msg || msg->state == STATE_ERROR)
  458. curr_cs_control(SSP_CHIP_DESELECT);
  459. }
  460. msg->state = NULL;
  461. if (msg->complete)
  462. msg->complete(msg->context);
  463. /* This message is completed, so let's turn off the clocks! */
  464. clk_disable(pl022->clk);
  465. amba_pclk_disable(pl022->adev);
  466. }
  467. /**
  468. * flush - flush the FIFO to reach a clean state
  469. * @pl022: SSP driver private data structure
  470. */
  471. static int flush(struct pl022 *pl022)
  472. {
  473. unsigned long limit = loops_per_jiffy << 1;
  474. dev_dbg(&pl022->adev->dev, "flush\n");
  475. do {
  476. while (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  477. readw(SSP_DR(pl022->virtbase));
  478. } while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_BSY) && limit--);
  479. pl022->exp_fifo_level = 0;
  480. return limit;
  481. }
  482. /**
  483. * restore_state - Load configuration of current chip
  484. * @pl022: SSP driver private data structure
  485. */
  486. static void restore_state(struct pl022 *pl022)
  487. {
  488. struct chip_data *chip = pl022->cur_chip;
  489. if (pl022->vendor->extended_cr)
  490. writel(chip->cr0, SSP_CR0(pl022->virtbase));
  491. else
  492. writew(chip->cr0, SSP_CR0(pl022->virtbase));
  493. writew(chip->cr1, SSP_CR1(pl022->virtbase));
  494. writew(chip->dmacr, SSP_DMACR(pl022->virtbase));
  495. writew(chip->cpsr, SSP_CPSR(pl022->virtbase));
  496. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  497. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  498. }
  499. /*
  500. * Default SSP Register Values
  501. */
  502. #define DEFAULT_SSP_REG_CR0 ( \
  503. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS, 0) | \
  504. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF, 4) | \
  505. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  506. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  507. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  508. )
  509. /* ST versions have slightly different bit layout */
  510. #define DEFAULT_SSP_REG_CR0_ST ( \
  511. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  512. GEN_MASK_BITS(SSP_MICROWIRE_CHANNEL_FULL_DUPLEX, SSP_CR0_MASK_HALFDUP_ST, 5) | \
  513. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  514. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  515. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) | \
  516. GEN_MASK_BITS(SSP_BITS_8, SSP_CR0_MASK_CSS_ST, 16) | \
  517. GEN_MASK_BITS(SSP_INTERFACE_MOTOROLA_SPI, SSP_CR0_MASK_FRF_ST, 21) \
  518. )
  519. /* The PL023 version is slightly different again */
  520. #define DEFAULT_SSP_REG_CR0_ST_PL023 ( \
  521. GEN_MASK_BITS(SSP_DATA_BITS_12, SSP_CR0_MASK_DSS_ST, 0) | \
  522. GEN_MASK_BITS(SSP_CLK_POL_IDLE_LOW, SSP_CR0_MASK_SPO, 6) | \
  523. GEN_MASK_BITS(SSP_CLK_SECOND_EDGE, SSP_CR0_MASK_SPH, 7) | \
  524. GEN_MASK_BITS(SSP_DEFAULT_CLKRATE, SSP_CR0_MASK_SCR, 8) \
  525. )
  526. #define DEFAULT_SSP_REG_CR1 ( \
  527. GEN_MASK_BITS(LOOPBACK_DISABLED, SSP_CR1_MASK_LBM, 0) | \
  528. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  529. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  530. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) \
  531. )
  532. /* ST versions extend this register to use all 16 bits */
  533. #define DEFAULT_SSP_REG_CR1_ST ( \
  534. DEFAULT_SSP_REG_CR1 | \
  535. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  536. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  537. GEN_MASK_BITS(SSP_MWIRE_WAIT_ZERO, SSP_CR1_MASK_MWAIT_ST, 6) |\
  538. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  539. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) \
  540. )
  541. /*
  542. * The PL023 variant has further differences: no loopback mode, no microwire
  543. * support, and a new clock feedback delay setting.
  544. */
  545. #define DEFAULT_SSP_REG_CR1_ST_PL023 ( \
  546. GEN_MASK_BITS(SSP_DISABLED, SSP_CR1_MASK_SSE, 1) | \
  547. GEN_MASK_BITS(SSP_MASTER, SSP_CR1_MASK_MS, 2) | \
  548. GEN_MASK_BITS(DO_NOT_DRIVE_TX, SSP_CR1_MASK_SOD, 3) | \
  549. GEN_MASK_BITS(SSP_RX_MSB, SSP_CR1_MASK_RENDN_ST, 4) | \
  550. GEN_MASK_BITS(SSP_TX_MSB, SSP_CR1_MASK_TENDN_ST, 5) | \
  551. GEN_MASK_BITS(SSP_RX_1_OR_MORE_ELEM, SSP_CR1_MASK_RXIFLSEL_ST, 7) | \
  552. GEN_MASK_BITS(SSP_TX_1_OR_MORE_EMPTY_LOC, SSP_CR1_MASK_TXIFLSEL_ST, 10) | \
  553. GEN_MASK_BITS(SSP_FEEDBACK_CLK_DELAY_NONE, SSP_CR1_MASK_FBCLKDEL_ST, 13) \
  554. )
  555. #define DEFAULT_SSP_REG_CPSR ( \
  556. GEN_MASK_BITS(SSP_DEFAULT_PRESCALE, SSP_CPSR_MASK_CPSDVSR, 0) \
  557. )
  558. #define DEFAULT_SSP_REG_DMACR (\
  559. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_RXDMAE, 0) | \
  560. GEN_MASK_BITS(SSP_DMA_DISABLED, SSP_DMACR_MASK_TXDMAE, 1) \
  561. )
  562. /**
  563. * load_ssp_default_config - Load default configuration for SSP
  564. * @pl022: SSP driver private data structure
  565. */
  566. static void load_ssp_default_config(struct pl022 *pl022)
  567. {
  568. if (pl022->vendor->pl023) {
  569. writel(DEFAULT_SSP_REG_CR0_ST_PL023, SSP_CR0(pl022->virtbase));
  570. writew(DEFAULT_SSP_REG_CR1_ST_PL023, SSP_CR1(pl022->virtbase));
  571. } else if (pl022->vendor->extended_cr) {
  572. writel(DEFAULT_SSP_REG_CR0_ST, SSP_CR0(pl022->virtbase));
  573. writew(DEFAULT_SSP_REG_CR1_ST, SSP_CR1(pl022->virtbase));
  574. } else {
  575. writew(DEFAULT_SSP_REG_CR0, SSP_CR0(pl022->virtbase));
  576. writew(DEFAULT_SSP_REG_CR1, SSP_CR1(pl022->virtbase));
  577. }
  578. writew(DEFAULT_SSP_REG_DMACR, SSP_DMACR(pl022->virtbase));
  579. writew(DEFAULT_SSP_REG_CPSR, SSP_CPSR(pl022->virtbase));
  580. writew(DISABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  581. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  582. }
  583. /**
  584. * This will write to TX and read from RX according to the parameters
  585. * set in pl022.
  586. */
  587. static void readwriter(struct pl022 *pl022)
  588. {
  589. /*
  590. * The FIFO depth is different inbetween primecell variants.
  591. * I believe filling in too much in the FIFO might cause
  592. * errons in 8bit wide transfers on ARM variants (just 8 words
  593. * FIFO, means only 8x8 = 64 bits in FIFO) at least.
  594. *
  595. * To prevent this issue, the TX FIFO is only filled to the
  596. * unused RX FIFO fill length, regardless of what the TX
  597. * FIFO status flag indicates.
  598. */
  599. dev_dbg(&pl022->adev->dev,
  600. "%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
  601. __func__, pl022->rx, pl022->rx_end, pl022->tx, pl022->tx_end);
  602. /* Read as much as you can */
  603. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  604. && (pl022->rx < pl022->rx_end)) {
  605. switch (pl022->read) {
  606. case READING_NULL:
  607. readw(SSP_DR(pl022->virtbase));
  608. break;
  609. case READING_U8:
  610. *(u8 *) (pl022->rx) =
  611. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  612. break;
  613. case READING_U16:
  614. *(u16 *) (pl022->rx) =
  615. (u16) readw(SSP_DR(pl022->virtbase));
  616. break;
  617. case READING_U32:
  618. *(u32 *) (pl022->rx) =
  619. readl(SSP_DR(pl022->virtbase));
  620. break;
  621. }
  622. pl022->rx += (pl022->cur_chip->n_bytes);
  623. pl022->exp_fifo_level--;
  624. }
  625. /*
  626. * Write as much as possible up to the RX FIFO size
  627. */
  628. while ((pl022->exp_fifo_level < pl022->vendor->fifodepth)
  629. && (pl022->tx < pl022->tx_end)) {
  630. switch (pl022->write) {
  631. case WRITING_NULL:
  632. writew(0x0, SSP_DR(pl022->virtbase));
  633. break;
  634. case WRITING_U8:
  635. writew(*(u8 *) (pl022->tx), SSP_DR(pl022->virtbase));
  636. break;
  637. case WRITING_U16:
  638. writew((*(u16 *) (pl022->tx)), SSP_DR(pl022->virtbase));
  639. break;
  640. case WRITING_U32:
  641. writel(*(u32 *) (pl022->tx), SSP_DR(pl022->virtbase));
  642. break;
  643. }
  644. pl022->tx += (pl022->cur_chip->n_bytes);
  645. pl022->exp_fifo_level++;
  646. /*
  647. * This inner reader takes care of things appearing in the RX
  648. * FIFO as we're transmitting. This will happen a lot since the
  649. * clock starts running when you put things into the TX FIFO,
  650. * and then things are continously clocked into the RX FIFO.
  651. */
  652. while ((readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RNE)
  653. && (pl022->rx < pl022->rx_end)) {
  654. switch (pl022->read) {
  655. case READING_NULL:
  656. readw(SSP_DR(pl022->virtbase));
  657. break;
  658. case READING_U8:
  659. *(u8 *) (pl022->rx) =
  660. readw(SSP_DR(pl022->virtbase)) & 0xFFU;
  661. break;
  662. case READING_U16:
  663. *(u16 *) (pl022->rx) =
  664. (u16) readw(SSP_DR(pl022->virtbase));
  665. break;
  666. case READING_U32:
  667. *(u32 *) (pl022->rx) =
  668. readl(SSP_DR(pl022->virtbase));
  669. break;
  670. }
  671. pl022->rx += (pl022->cur_chip->n_bytes);
  672. pl022->exp_fifo_level--;
  673. }
  674. }
  675. /*
  676. * When we exit here the TX FIFO should be full and the RX FIFO
  677. * should be empty
  678. */
  679. }
  680. /**
  681. * next_transfer - Move to the Next transfer in the current spi message
  682. * @pl022: SSP driver private data structure
  683. *
  684. * This function moves though the linked list of spi transfers in the
  685. * current spi message and returns with the state of current spi
  686. * message i.e whether its last transfer is done(STATE_DONE) or
  687. * Next transfer is ready(STATE_RUNNING)
  688. */
  689. static void *next_transfer(struct pl022 *pl022)
  690. {
  691. struct spi_message *msg = pl022->cur_msg;
  692. struct spi_transfer *trans = pl022->cur_transfer;
  693. /* Move to next transfer */
  694. if (trans->transfer_list.next != &msg->transfers) {
  695. pl022->cur_transfer =
  696. list_entry(trans->transfer_list.next,
  697. struct spi_transfer, transfer_list);
  698. return STATE_RUNNING;
  699. }
  700. return STATE_DONE;
  701. }
  702. /*
  703. * This DMA functionality is only compiled in if we have
  704. * access to the generic DMA devices/DMA engine.
  705. */
  706. #ifdef CONFIG_DMA_ENGINE
  707. static void unmap_free_dma_scatter(struct pl022 *pl022)
  708. {
  709. /* Unmap and free the SG tables */
  710. dma_unmap_sg(pl022->dma_tx_channel->device->dev, pl022->sgt_tx.sgl,
  711. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  712. dma_unmap_sg(pl022->dma_rx_channel->device->dev, pl022->sgt_rx.sgl,
  713. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  714. sg_free_table(&pl022->sgt_rx);
  715. sg_free_table(&pl022->sgt_tx);
  716. }
  717. static void dma_callback(void *data)
  718. {
  719. struct pl022 *pl022 = data;
  720. struct spi_message *msg = pl022->cur_msg;
  721. BUG_ON(!pl022->sgt_rx.sgl);
  722. #ifdef VERBOSE_DEBUG
  723. /*
  724. * Optionally dump out buffers to inspect contents, this is
  725. * good if you want to convince yourself that the loopback
  726. * read/write contents are the same, when adopting to a new
  727. * DMA engine.
  728. */
  729. {
  730. struct scatterlist *sg;
  731. unsigned int i;
  732. dma_sync_sg_for_cpu(&pl022->adev->dev,
  733. pl022->sgt_rx.sgl,
  734. pl022->sgt_rx.nents,
  735. DMA_FROM_DEVICE);
  736. for_each_sg(pl022->sgt_rx.sgl, sg, pl022->sgt_rx.nents, i) {
  737. dev_dbg(&pl022->adev->dev, "SPI RX SG ENTRY: %d", i);
  738. print_hex_dump(KERN_ERR, "SPI RX: ",
  739. DUMP_PREFIX_OFFSET,
  740. 16,
  741. 1,
  742. sg_virt(sg),
  743. sg_dma_len(sg),
  744. 1);
  745. }
  746. for_each_sg(pl022->sgt_tx.sgl, sg, pl022->sgt_tx.nents, i) {
  747. dev_dbg(&pl022->adev->dev, "SPI TX SG ENTRY: %d", i);
  748. print_hex_dump(KERN_ERR, "SPI TX: ",
  749. DUMP_PREFIX_OFFSET,
  750. 16,
  751. 1,
  752. sg_virt(sg),
  753. sg_dma_len(sg),
  754. 1);
  755. }
  756. }
  757. #endif
  758. unmap_free_dma_scatter(pl022);
  759. /* Update total bytes transfered */
  760. msg->actual_length += pl022->cur_transfer->len;
  761. if (pl022->cur_transfer->cs_change)
  762. pl022->cur_chip->
  763. cs_control(SSP_CHIP_DESELECT);
  764. /* Move to next transfer */
  765. msg->state = next_transfer(pl022);
  766. tasklet_schedule(&pl022->pump_transfers);
  767. }
  768. static void setup_dma_scatter(struct pl022 *pl022,
  769. void *buffer,
  770. unsigned int length,
  771. struct sg_table *sgtab)
  772. {
  773. struct scatterlist *sg;
  774. int bytesleft = length;
  775. void *bufp = buffer;
  776. int mapbytes;
  777. int i;
  778. if (buffer) {
  779. for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
  780. /*
  781. * If there are less bytes left than what fits
  782. * in the current page (plus page alignment offset)
  783. * we just feed in this, else we stuff in as much
  784. * as we can.
  785. */
  786. if (bytesleft < (PAGE_SIZE - offset_in_page(bufp)))
  787. mapbytes = bytesleft;
  788. else
  789. mapbytes = PAGE_SIZE - offset_in_page(bufp);
  790. sg_set_page(sg, virt_to_page(bufp),
  791. mapbytes, offset_in_page(bufp));
  792. bufp += mapbytes;
  793. bytesleft -= mapbytes;
  794. dev_dbg(&pl022->adev->dev,
  795. "set RX/TX target page @ %p, %d bytes, %d left\n",
  796. bufp, mapbytes, bytesleft);
  797. }
  798. } else {
  799. /* Map the dummy buffer on every page */
  800. for_each_sg(sgtab->sgl, sg, sgtab->nents, i) {
  801. if (bytesleft < PAGE_SIZE)
  802. mapbytes = bytesleft;
  803. else
  804. mapbytes = PAGE_SIZE;
  805. sg_set_page(sg, virt_to_page(pl022->dummypage),
  806. mapbytes, 0);
  807. bytesleft -= mapbytes;
  808. dev_dbg(&pl022->adev->dev,
  809. "set RX/TX to dummy page %d bytes, %d left\n",
  810. mapbytes, bytesleft);
  811. }
  812. }
  813. BUG_ON(bytesleft);
  814. }
  815. /**
  816. * configure_dma - configures the channels for the next transfer
  817. * @pl022: SSP driver's private data structure
  818. */
  819. static int configure_dma(struct pl022 *pl022)
  820. {
  821. struct dma_slave_config rx_conf = {
  822. .src_addr = SSP_DR(pl022->phybase),
  823. .direction = DMA_FROM_DEVICE,
  824. .src_maxburst = pl022->vendor->fifodepth >> 1,
  825. };
  826. struct dma_slave_config tx_conf = {
  827. .dst_addr = SSP_DR(pl022->phybase),
  828. .direction = DMA_TO_DEVICE,
  829. .dst_maxburst = pl022->vendor->fifodepth >> 1,
  830. };
  831. unsigned int pages;
  832. int ret;
  833. int rx_sglen, tx_sglen;
  834. struct dma_chan *rxchan = pl022->dma_rx_channel;
  835. struct dma_chan *txchan = pl022->dma_tx_channel;
  836. struct dma_async_tx_descriptor *rxdesc;
  837. struct dma_async_tx_descriptor *txdesc;
  838. /* Check that the channels are available */
  839. if (!rxchan || !txchan)
  840. return -ENODEV;
  841. switch (pl022->read) {
  842. case READING_NULL:
  843. /* Use the same as for writing */
  844. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  845. break;
  846. case READING_U8:
  847. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  848. break;
  849. case READING_U16:
  850. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  851. break;
  852. case READING_U32:
  853. rx_conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  854. break;
  855. }
  856. switch (pl022->write) {
  857. case WRITING_NULL:
  858. /* Use the same as for reading */
  859. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
  860. break;
  861. case WRITING_U8:
  862. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  863. break;
  864. case WRITING_U16:
  865. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
  866. break;
  867. case WRITING_U32:
  868. tx_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  869. break;
  870. }
  871. /* SPI pecularity: we need to read and write the same width */
  872. if (rx_conf.src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  873. rx_conf.src_addr_width = tx_conf.dst_addr_width;
  874. if (tx_conf.dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  875. tx_conf.dst_addr_width = rx_conf.src_addr_width;
  876. BUG_ON(rx_conf.src_addr_width != tx_conf.dst_addr_width);
  877. dmaengine_slave_config(rxchan, &rx_conf);
  878. dmaengine_slave_config(txchan, &tx_conf);
  879. /* Create sglists for the transfers */
  880. pages = (pl022->cur_transfer->len >> PAGE_SHIFT) + 1;
  881. dev_dbg(&pl022->adev->dev, "using %d pages for transfer\n", pages);
  882. ret = sg_alloc_table(&pl022->sgt_rx, pages, GFP_KERNEL);
  883. if (ret)
  884. goto err_alloc_rx_sg;
  885. ret = sg_alloc_table(&pl022->sgt_tx, pages, GFP_KERNEL);
  886. if (ret)
  887. goto err_alloc_tx_sg;
  888. /* Fill in the scatterlists for the RX+TX buffers */
  889. setup_dma_scatter(pl022, pl022->rx,
  890. pl022->cur_transfer->len, &pl022->sgt_rx);
  891. setup_dma_scatter(pl022, pl022->tx,
  892. pl022->cur_transfer->len, &pl022->sgt_tx);
  893. /* Map DMA buffers */
  894. rx_sglen = dma_map_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
  895. pl022->sgt_rx.nents, DMA_FROM_DEVICE);
  896. if (!rx_sglen)
  897. goto err_rx_sgmap;
  898. tx_sglen = dma_map_sg(txchan->device->dev, pl022->sgt_tx.sgl,
  899. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  900. if (!tx_sglen)
  901. goto err_tx_sgmap;
  902. /* Send both scatterlists */
  903. rxdesc = rxchan->device->device_prep_slave_sg(rxchan,
  904. pl022->sgt_rx.sgl,
  905. rx_sglen,
  906. DMA_FROM_DEVICE,
  907. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  908. if (!rxdesc)
  909. goto err_rxdesc;
  910. txdesc = txchan->device->device_prep_slave_sg(txchan,
  911. pl022->sgt_tx.sgl,
  912. tx_sglen,
  913. DMA_TO_DEVICE,
  914. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  915. if (!txdesc)
  916. goto err_txdesc;
  917. /* Put the callback on the RX transfer only, that should finish last */
  918. rxdesc->callback = dma_callback;
  919. rxdesc->callback_param = pl022;
  920. /* Submit and fire RX and TX with TX last so we're ready to read! */
  921. dmaengine_submit(rxdesc);
  922. dmaengine_submit(txdesc);
  923. dma_async_issue_pending(rxchan);
  924. dma_async_issue_pending(txchan);
  925. return 0;
  926. err_submit_tx:
  927. err_submit_rx:
  928. err_txdesc:
  929. dmaengine_terminate_all(txchan);
  930. err_rxdesc:
  931. dmaengine_terminate_all(rxchan);
  932. dma_unmap_sg(txchan->device->dev, pl022->sgt_tx.sgl,
  933. pl022->sgt_tx.nents, DMA_TO_DEVICE);
  934. err_tx_sgmap:
  935. dma_unmap_sg(rxchan->device->dev, pl022->sgt_rx.sgl,
  936. pl022->sgt_tx.nents, DMA_FROM_DEVICE);
  937. err_rx_sgmap:
  938. sg_free_table(&pl022->sgt_tx);
  939. err_alloc_tx_sg:
  940. sg_free_table(&pl022->sgt_rx);
  941. err_alloc_rx_sg:
  942. return -ENOMEM;
  943. }
  944. static int __init pl022_dma_probe(struct pl022 *pl022)
  945. {
  946. dma_cap_mask_t mask;
  947. /* Try to acquire a generic DMA engine slave channel */
  948. dma_cap_zero(mask);
  949. dma_cap_set(DMA_SLAVE, mask);
  950. /*
  951. * We need both RX and TX channels to do DMA, else do none
  952. * of them.
  953. */
  954. pl022->dma_rx_channel = dma_request_channel(mask,
  955. pl022->master_info->dma_filter,
  956. pl022->master_info->dma_rx_param);
  957. if (!pl022->dma_rx_channel) {
  958. dev_err(&pl022->adev->dev, "no RX DMA channel!\n");
  959. goto err_no_rxchan;
  960. }
  961. pl022->dma_tx_channel = dma_request_channel(mask,
  962. pl022->master_info->dma_filter,
  963. pl022->master_info->dma_tx_param);
  964. if (!pl022->dma_tx_channel) {
  965. dev_err(&pl022->adev->dev, "no TX DMA channel!\n");
  966. goto err_no_txchan;
  967. }
  968. pl022->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
  969. if (!pl022->dummypage) {
  970. dev_err(&pl022->adev->dev, "no DMA dummypage!\n");
  971. goto err_no_dummypage;
  972. }
  973. dev_info(&pl022->adev->dev, "setup for DMA on RX %s, TX %s\n",
  974. dma_chan_name(pl022->dma_rx_channel),
  975. dma_chan_name(pl022->dma_tx_channel));
  976. return 0;
  977. err_no_dummypage:
  978. dma_release_channel(pl022->dma_tx_channel);
  979. err_no_txchan:
  980. dma_release_channel(pl022->dma_rx_channel);
  981. pl022->dma_rx_channel = NULL;
  982. err_no_rxchan:
  983. return -ENODEV;
  984. }
  985. static void terminate_dma(struct pl022 *pl022)
  986. {
  987. struct dma_chan *rxchan = pl022->dma_rx_channel;
  988. struct dma_chan *txchan = pl022->dma_tx_channel;
  989. dmaengine_terminate_all(rxchan);
  990. dmaengine_terminate_all(txchan);
  991. unmap_free_dma_scatter(pl022);
  992. }
  993. static void pl022_dma_remove(struct pl022 *pl022)
  994. {
  995. if (pl022->busy)
  996. terminate_dma(pl022);
  997. if (pl022->dma_tx_channel)
  998. dma_release_channel(pl022->dma_tx_channel);
  999. if (pl022->dma_rx_channel)
  1000. dma_release_channel(pl022->dma_rx_channel);
  1001. kfree(pl022->dummypage);
  1002. }
  1003. #else
  1004. static inline int configure_dma(struct pl022 *pl022)
  1005. {
  1006. return -ENODEV;
  1007. }
  1008. static inline int pl022_dma_probe(struct pl022 *pl022)
  1009. {
  1010. return 0;
  1011. }
  1012. static inline void pl022_dma_remove(struct pl022 *pl022)
  1013. {
  1014. }
  1015. #endif
  1016. /**
  1017. * pl022_interrupt_handler - Interrupt handler for SSP controller
  1018. *
  1019. * This function handles interrupts generated for an interrupt based transfer.
  1020. * If a receive overrun (ROR) interrupt is there then we disable SSP, flag the
  1021. * current message's state as STATE_ERROR and schedule the tasklet
  1022. * pump_transfers which will do the postprocessing of the current message by
  1023. * calling giveback(). Otherwise it reads data from RX FIFO till there is no
  1024. * more data, and writes data in TX FIFO till it is not full. If we complete
  1025. * the transfer we move to the next transfer and schedule the tasklet.
  1026. */
  1027. static irqreturn_t pl022_interrupt_handler(int irq, void *dev_id)
  1028. {
  1029. struct pl022 *pl022 = dev_id;
  1030. struct spi_message *msg = pl022->cur_msg;
  1031. u16 irq_status = 0;
  1032. u16 flag = 0;
  1033. if (unlikely(!msg)) {
  1034. dev_err(&pl022->adev->dev,
  1035. "bad message state in interrupt handler");
  1036. /* Never fail */
  1037. return IRQ_HANDLED;
  1038. }
  1039. /* Read the Interrupt Status Register */
  1040. irq_status = readw(SSP_MIS(pl022->virtbase));
  1041. if (unlikely(!irq_status))
  1042. return IRQ_NONE;
  1043. /*
  1044. * This handles the FIFO interrupts, the timeout
  1045. * interrupts are flatly ignored, they cannot be
  1046. * trusted.
  1047. */
  1048. if (unlikely(irq_status & SSP_MIS_MASK_RORMIS)) {
  1049. /*
  1050. * Overrun interrupt - bail out since our Data has been
  1051. * corrupted
  1052. */
  1053. dev_err(&pl022->adev->dev, "FIFO overrun\n");
  1054. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_RFF)
  1055. dev_err(&pl022->adev->dev,
  1056. "RXFIFO is full\n");
  1057. if (readw(SSP_SR(pl022->virtbase)) & SSP_SR_MASK_TNF)
  1058. dev_err(&pl022->adev->dev,
  1059. "TXFIFO is full\n");
  1060. /*
  1061. * Disable and clear interrupts, disable SSP,
  1062. * mark message with bad status so it can be
  1063. * retried.
  1064. */
  1065. writew(DISABLE_ALL_INTERRUPTS,
  1066. SSP_IMSC(pl022->virtbase));
  1067. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  1068. writew((readw(SSP_CR1(pl022->virtbase)) &
  1069. (~SSP_CR1_MASK_SSE)), SSP_CR1(pl022->virtbase));
  1070. msg->state = STATE_ERROR;
  1071. /* Schedule message queue handler */
  1072. tasklet_schedule(&pl022->pump_transfers);
  1073. return IRQ_HANDLED;
  1074. }
  1075. readwriter(pl022);
  1076. if ((pl022->tx == pl022->tx_end) && (flag == 0)) {
  1077. flag = 1;
  1078. /* Disable Transmit interrupt */
  1079. writew(readw(SSP_IMSC(pl022->virtbase)) &
  1080. (~SSP_IMSC_MASK_TXIM),
  1081. SSP_IMSC(pl022->virtbase));
  1082. }
  1083. /*
  1084. * Since all transactions must write as much as shall be read,
  1085. * we can conclude the entire transaction once RX is complete.
  1086. * At this point, all TX will always be finished.
  1087. */
  1088. if (pl022->rx >= pl022->rx_end) {
  1089. writew(DISABLE_ALL_INTERRUPTS,
  1090. SSP_IMSC(pl022->virtbase));
  1091. writew(CLEAR_ALL_INTERRUPTS, SSP_ICR(pl022->virtbase));
  1092. if (unlikely(pl022->rx > pl022->rx_end)) {
  1093. dev_warn(&pl022->adev->dev, "read %u surplus "
  1094. "bytes (did you request an odd "
  1095. "number of bytes on a 16bit bus?)\n",
  1096. (u32) (pl022->rx - pl022->rx_end));
  1097. }
  1098. /* Update total bytes transfered */
  1099. msg->actual_length += pl022->cur_transfer->len;
  1100. if (pl022->cur_transfer->cs_change)
  1101. pl022->cur_chip->
  1102. cs_control(SSP_CHIP_DESELECT);
  1103. /* Move to next transfer */
  1104. msg->state = next_transfer(pl022);
  1105. tasklet_schedule(&pl022->pump_transfers);
  1106. return IRQ_HANDLED;
  1107. }
  1108. return IRQ_HANDLED;
  1109. }
  1110. /**
  1111. * This sets up the pointers to memory for the next message to
  1112. * send out on the SPI bus.
  1113. */
  1114. static int set_up_next_transfer(struct pl022 *pl022,
  1115. struct spi_transfer *transfer)
  1116. {
  1117. int residue;
  1118. /* Sanity check the message for this bus width */
  1119. residue = pl022->cur_transfer->len % pl022->cur_chip->n_bytes;
  1120. if (unlikely(residue != 0)) {
  1121. dev_err(&pl022->adev->dev,
  1122. "message of %u bytes to transmit but the current "
  1123. "chip bus has a data width of %u bytes!\n",
  1124. pl022->cur_transfer->len,
  1125. pl022->cur_chip->n_bytes);
  1126. dev_err(&pl022->adev->dev, "skipping this message\n");
  1127. return -EIO;
  1128. }
  1129. pl022->tx = (void *)transfer->tx_buf;
  1130. pl022->tx_end = pl022->tx + pl022->cur_transfer->len;
  1131. pl022->rx = (void *)transfer->rx_buf;
  1132. pl022->rx_end = pl022->rx + pl022->cur_transfer->len;
  1133. pl022->write =
  1134. pl022->tx ? pl022->cur_chip->write : WRITING_NULL;
  1135. pl022->read = pl022->rx ? pl022->cur_chip->read : READING_NULL;
  1136. return 0;
  1137. }
  1138. /**
  1139. * pump_transfers - Tasklet function which schedules next transfer
  1140. * when running in interrupt or DMA transfer mode.
  1141. * @data: SSP driver private data structure
  1142. *
  1143. */
  1144. static void pump_transfers(unsigned long data)
  1145. {
  1146. struct pl022 *pl022 = (struct pl022 *) data;
  1147. struct spi_message *message = NULL;
  1148. struct spi_transfer *transfer = NULL;
  1149. struct spi_transfer *previous = NULL;
  1150. /* Get current state information */
  1151. message = pl022->cur_msg;
  1152. transfer = pl022->cur_transfer;
  1153. /* Handle for abort */
  1154. if (message->state == STATE_ERROR) {
  1155. message->status = -EIO;
  1156. giveback(pl022);
  1157. return;
  1158. }
  1159. /* Handle end of message */
  1160. if (message->state == STATE_DONE) {
  1161. message->status = 0;
  1162. giveback(pl022);
  1163. return;
  1164. }
  1165. /* Delay if requested at end of transfer before CS change */
  1166. if (message->state == STATE_RUNNING) {
  1167. previous = list_entry(transfer->transfer_list.prev,
  1168. struct spi_transfer,
  1169. transfer_list);
  1170. if (previous->delay_usecs)
  1171. /*
  1172. * FIXME: This runs in interrupt context.
  1173. * Is this really smart?
  1174. */
  1175. udelay(previous->delay_usecs);
  1176. /* Drop chip select only if cs_change is requested */
  1177. if (previous->cs_change)
  1178. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1179. } else {
  1180. /* STATE_START */
  1181. message->state = STATE_RUNNING;
  1182. }
  1183. if (set_up_next_transfer(pl022, transfer)) {
  1184. message->state = STATE_ERROR;
  1185. message->status = -EIO;
  1186. giveback(pl022);
  1187. return;
  1188. }
  1189. /* Flush the FIFOs and let's go! */
  1190. flush(pl022);
  1191. if (pl022->cur_chip->enable_dma) {
  1192. if (configure_dma(pl022)) {
  1193. dev_dbg(&pl022->adev->dev,
  1194. "configuration of DMA failed, fall back to interrupt mode\n");
  1195. goto err_config_dma;
  1196. }
  1197. return;
  1198. }
  1199. err_config_dma:
  1200. writew(ENABLE_ALL_INTERRUPTS, SSP_IMSC(pl022->virtbase));
  1201. }
  1202. static void do_interrupt_dma_transfer(struct pl022 *pl022)
  1203. {
  1204. u32 irqflags = ENABLE_ALL_INTERRUPTS;
  1205. /* Enable target chip */
  1206. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1207. if (set_up_next_transfer(pl022, pl022->cur_transfer)) {
  1208. /* Error path */
  1209. pl022->cur_msg->state = STATE_ERROR;
  1210. pl022->cur_msg->status = -EIO;
  1211. giveback(pl022);
  1212. return;
  1213. }
  1214. /* If we're using DMA, set up DMA here */
  1215. if (pl022->cur_chip->enable_dma) {
  1216. /* Configure DMA transfer */
  1217. if (configure_dma(pl022)) {
  1218. dev_dbg(&pl022->adev->dev,
  1219. "configuration of DMA failed, fall back to interrupt mode\n");
  1220. goto err_config_dma;
  1221. }
  1222. /* Disable interrupts in DMA mode, IRQ from DMA controller */
  1223. irqflags = DISABLE_ALL_INTERRUPTS;
  1224. }
  1225. err_config_dma:
  1226. /* Enable SSP, turn on interrupts */
  1227. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  1228. SSP_CR1(pl022->virtbase));
  1229. writew(irqflags, SSP_IMSC(pl022->virtbase));
  1230. }
  1231. static void do_polling_transfer(struct pl022 *pl022)
  1232. {
  1233. struct spi_message *message = NULL;
  1234. struct spi_transfer *transfer = NULL;
  1235. struct spi_transfer *previous = NULL;
  1236. struct chip_data *chip;
  1237. chip = pl022->cur_chip;
  1238. message = pl022->cur_msg;
  1239. while (message->state != STATE_DONE) {
  1240. /* Handle for abort */
  1241. if (message->state == STATE_ERROR)
  1242. break;
  1243. transfer = pl022->cur_transfer;
  1244. /* Delay if requested at end of transfer */
  1245. if (message->state == STATE_RUNNING) {
  1246. previous =
  1247. list_entry(transfer->transfer_list.prev,
  1248. struct spi_transfer, transfer_list);
  1249. if (previous->delay_usecs)
  1250. udelay(previous->delay_usecs);
  1251. if (previous->cs_change)
  1252. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1253. } else {
  1254. /* STATE_START */
  1255. message->state = STATE_RUNNING;
  1256. pl022->cur_chip->cs_control(SSP_CHIP_SELECT);
  1257. }
  1258. /* Configuration Changing Per Transfer */
  1259. if (set_up_next_transfer(pl022, transfer)) {
  1260. /* Error path */
  1261. message->state = STATE_ERROR;
  1262. break;
  1263. }
  1264. /* Flush FIFOs and enable SSP */
  1265. flush(pl022);
  1266. writew((readw(SSP_CR1(pl022->virtbase)) | SSP_CR1_MASK_SSE),
  1267. SSP_CR1(pl022->virtbase));
  1268. dev_dbg(&pl022->adev->dev, "polling transfer ongoing ...\n");
  1269. /* FIXME: insert a timeout so we don't hang here indefinately */
  1270. while (pl022->tx < pl022->tx_end || pl022->rx < pl022->rx_end)
  1271. readwriter(pl022);
  1272. /* Update total byte transfered */
  1273. message->actual_length += pl022->cur_transfer->len;
  1274. if (pl022->cur_transfer->cs_change)
  1275. pl022->cur_chip->cs_control(SSP_CHIP_DESELECT);
  1276. /* Move to next transfer */
  1277. message->state = next_transfer(pl022);
  1278. }
  1279. /* Handle end of message */
  1280. if (message->state == STATE_DONE)
  1281. message->status = 0;
  1282. else
  1283. message->status = -EIO;
  1284. giveback(pl022);
  1285. return;
  1286. }
  1287. /**
  1288. * pump_messages - Workqueue function which processes spi message queue
  1289. * @data: pointer to private data of SSP driver
  1290. *
  1291. * This function checks if there is any spi message in the queue that
  1292. * needs processing and delegate control to appropriate function
  1293. * do_polling_transfer()/do_interrupt_dma_transfer()
  1294. * based on the kind of the transfer
  1295. *
  1296. */
  1297. static void pump_messages(struct work_struct *work)
  1298. {
  1299. struct pl022 *pl022 =
  1300. container_of(work, struct pl022, pump_messages);
  1301. unsigned long flags;
  1302. /* Lock queue and check for queue work */
  1303. spin_lock_irqsave(&pl022->queue_lock, flags);
  1304. if (list_empty(&pl022->queue) || !pl022->running) {
  1305. pl022->busy = false;
  1306. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1307. return;
  1308. }
  1309. /* Make sure we are not already running a message */
  1310. if (pl022->cur_msg) {
  1311. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1312. return;
  1313. }
  1314. /* Extract head of queue */
  1315. pl022->cur_msg =
  1316. list_entry(pl022->queue.next, struct spi_message, queue);
  1317. list_del_init(&pl022->cur_msg->queue);
  1318. pl022->busy = true;
  1319. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1320. /* Initial message state */
  1321. pl022->cur_msg->state = STATE_START;
  1322. pl022->cur_transfer = list_entry(pl022->cur_msg->transfers.next,
  1323. struct spi_transfer,
  1324. transfer_list);
  1325. /* Setup the SPI using the per chip configuration */
  1326. pl022->cur_chip = spi_get_ctldata(pl022->cur_msg->spi);
  1327. /*
  1328. * We enable the clocks here, then the clocks will be disabled when
  1329. * giveback() is called in each method (poll/interrupt/DMA)
  1330. */
  1331. amba_pclk_enable(pl022->adev);
  1332. clk_enable(pl022->clk);
  1333. restore_state(pl022);
  1334. flush(pl022);
  1335. if (pl022->cur_chip->xfer_type == POLLING_TRANSFER)
  1336. do_polling_transfer(pl022);
  1337. else
  1338. do_interrupt_dma_transfer(pl022);
  1339. }
  1340. static int __init init_queue(struct pl022 *pl022)
  1341. {
  1342. INIT_LIST_HEAD(&pl022->queue);
  1343. spin_lock_init(&pl022->queue_lock);
  1344. pl022->running = false;
  1345. pl022->busy = false;
  1346. tasklet_init(&pl022->pump_transfers,
  1347. pump_transfers, (unsigned long)pl022);
  1348. INIT_WORK(&pl022->pump_messages, pump_messages);
  1349. pl022->workqueue = create_singlethread_workqueue(
  1350. dev_name(pl022->master->dev.parent));
  1351. if (pl022->workqueue == NULL)
  1352. return -EBUSY;
  1353. return 0;
  1354. }
  1355. static int start_queue(struct pl022 *pl022)
  1356. {
  1357. unsigned long flags;
  1358. spin_lock_irqsave(&pl022->queue_lock, flags);
  1359. if (pl022->running || pl022->busy) {
  1360. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1361. return -EBUSY;
  1362. }
  1363. pl022->running = true;
  1364. pl022->cur_msg = NULL;
  1365. pl022->cur_transfer = NULL;
  1366. pl022->cur_chip = NULL;
  1367. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1368. queue_work(pl022->workqueue, &pl022->pump_messages);
  1369. return 0;
  1370. }
  1371. static int stop_queue(struct pl022 *pl022)
  1372. {
  1373. unsigned long flags;
  1374. unsigned limit = 500;
  1375. int status = 0;
  1376. spin_lock_irqsave(&pl022->queue_lock, flags);
  1377. /* This is a bit lame, but is optimized for the common execution path.
  1378. * A wait_queue on the pl022->busy could be used, but then the common
  1379. * execution path (pump_messages) would be required to call wake_up or
  1380. * friends on every SPI message. Do this instead */
  1381. while (!list_empty(&pl022->queue) && pl022->busy && limit--) {
  1382. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1383. msleep(10);
  1384. spin_lock_irqsave(&pl022->queue_lock, flags);
  1385. }
  1386. if (!list_empty(&pl022->queue) || pl022->busy)
  1387. status = -EBUSY;
  1388. else
  1389. pl022->running = false;
  1390. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1391. return status;
  1392. }
  1393. static int destroy_queue(struct pl022 *pl022)
  1394. {
  1395. int status;
  1396. status = stop_queue(pl022);
  1397. /* we are unloading the module or failing to load (only two calls
  1398. * to this routine), and neither call can handle a return value.
  1399. * However, destroy_workqueue calls flush_workqueue, and that will
  1400. * block until all work is done. If the reason that stop_queue
  1401. * timed out is that the work will never finish, then it does no
  1402. * good to call destroy_workqueue, so return anyway. */
  1403. if (status != 0)
  1404. return status;
  1405. destroy_workqueue(pl022->workqueue);
  1406. return 0;
  1407. }
  1408. static int verify_controller_parameters(struct pl022 *pl022,
  1409. struct pl022_config_chip const *chip_info)
  1410. {
  1411. if ((chip_info->iface < SSP_INTERFACE_MOTOROLA_SPI)
  1412. || (chip_info->iface > SSP_INTERFACE_UNIDIRECTIONAL)) {
  1413. dev_err(&pl022->adev->dev,
  1414. "interface is configured incorrectly\n");
  1415. return -EINVAL;
  1416. }
  1417. if ((chip_info->iface == SSP_INTERFACE_UNIDIRECTIONAL) &&
  1418. (!pl022->vendor->unidir)) {
  1419. dev_err(&pl022->adev->dev,
  1420. "unidirectional mode not supported in this "
  1421. "hardware version\n");
  1422. return -EINVAL;
  1423. }
  1424. if ((chip_info->hierarchy != SSP_MASTER)
  1425. && (chip_info->hierarchy != SSP_SLAVE)) {
  1426. dev_err(&pl022->adev->dev,
  1427. "hierarchy is configured incorrectly\n");
  1428. return -EINVAL;
  1429. }
  1430. if ((chip_info->com_mode != INTERRUPT_TRANSFER)
  1431. && (chip_info->com_mode != DMA_TRANSFER)
  1432. && (chip_info->com_mode != POLLING_TRANSFER)) {
  1433. dev_err(&pl022->adev->dev,
  1434. "Communication mode is configured incorrectly\n");
  1435. return -EINVAL;
  1436. }
  1437. if ((chip_info->rx_lev_trig < SSP_RX_1_OR_MORE_ELEM)
  1438. || (chip_info->rx_lev_trig > SSP_RX_32_OR_MORE_ELEM)) {
  1439. dev_err(&pl022->adev->dev,
  1440. "RX FIFO Trigger Level is configured incorrectly\n");
  1441. return -EINVAL;
  1442. }
  1443. if ((chip_info->tx_lev_trig < SSP_TX_1_OR_MORE_EMPTY_LOC)
  1444. || (chip_info->tx_lev_trig > SSP_TX_32_OR_MORE_EMPTY_LOC)) {
  1445. dev_err(&pl022->adev->dev,
  1446. "TX FIFO Trigger Level is configured incorrectly\n");
  1447. return -EINVAL;
  1448. }
  1449. if (chip_info->iface == SSP_INTERFACE_NATIONAL_MICROWIRE) {
  1450. if ((chip_info->ctrl_len < SSP_BITS_4)
  1451. || (chip_info->ctrl_len > SSP_BITS_32)) {
  1452. dev_err(&pl022->adev->dev,
  1453. "CTRL LEN is configured incorrectly\n");
  1454. return -EINVAL;
  1455. }
  1456. if ((chip_info->wait_state != SSP_MWIRE_WAIT_ZERO)
  1457. && (chip_info->wait_state != SSP_MWIRE_WAIT_ONE)) {
  1458. dev_err(&pl022->adev->dev,
  1459. "Wait State is configured incorrectly\n");
  1460. return -EINVAL;
  1461. }
  1462. /* Half duplex is only available in the ST Micro version */
  1463. if (pl022->vendor->extended_cr) {
  1464. if ((chip_info->duplex !=
  1465. SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1466. && (chip_info->duplex !=
  1467. SSP_MICROWIRE_CHANNEL_HALF_DUPLEX)) {
  1468. dev_err(&pl022->adev->dev,
  1469. "Microwire duplex mode is configured incorrectly\n");
  1470. return -EINVAL;
  1471. }
  1472. } else {
  1473. if (chip_info->duplex != SSP_MICROWIRE_CHANNEL_FULL_DUPLEX)
  1474. dev_err(&pl022->adev->dev,
  1475. "Microwire half duplex mode requested,"
  1476. " but this is only available in the"
  1477. " ST version of PL022\n");
  1478. return -EINVAL;
  1479. }
  1480. }
  1481. return 0;
  1482. }
  1483. /**
  1484. * pl022_transfer - transfer function registered to SPI master framework
  1485. * @spi: spi device which is requesting transfer
  1486. * @msg: spi message which is to handled is queued to driver queue
  1487. *
  1488. * This function is registered to the SPI framework for this SPI master
  1489. * controller. It will queue the spi_message in the queue of driver if
  1490. * the queue is not stopped and return.
  1491. */
  1492. static int pl022_transfer(struct spi_device *spi, struct spi_message *msg)
  1493. {
  1494. struct pl022 *pl022 = spi_master_get_devdata(spi->master);
  1495. unsigned long flags;
  1496. spin_lock_irqsave(&pl022->queue_lock, flags);
  1497. if (!pl022->running) {
  1498. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1499. return -ESHUTDOWN;
  1500. }
  1501. msg->actual_length = 0;
  1502. msg->status = -EINPROGRESS;
  1503. msg->state = STATE_START;
  1504. list_add_tail(&msg->queue, &pl022->queue);
  1505. if (pl022->running && !pl022->busy)
  1506. queue_work(pl022->workqueue, &pl022->pump_messages);
  1507. spin_unlock_irqrestore(&pl022->queue_lock, flags);
  1508. return 0;
  1509. }
  1510. static int calculate_effective_freq(struct pl022 *pl022,
  1511. int freq,
  1512. struct ssp_clock_params *clk_freq)
  1513. {
  1514. /* Lets calculate the frequency parameters */
  1515. u16 cpsdvsr = 2;
  1516. u16 scr = 0;
  1517. bool freq_found = false;
  1518. u32 rate;
  1519. u32 max_tclk;
  1520. u32 min_tclk;
  1521. rate = clk_get_rate(pl022->clk);
  1522. /* cpsdvscr = 2 & scr 0 */
  1523. max_tclk = (rate / (CPSDVR_MIN * (1 + SCR_MIN)));
  1524. /* cpsdvsr = 254 & scr = 255 */
  1525. min_tclk = (rate / (CPSDVR_MAX * (1 + SCR_MAX)));
  1526. if ((freq <= max_tclk) && (freq >= min_tclk)) {
  1527. while (cpsdvsr <= CPSDVR_MAX && !freq_found) {
  1528. while (scr <= SCR_MAX && !freq_found) {
  1529. if ((rate /
  1530. (cpsdvsr * (1 + scr))) > freq)
  1531. scr += 1;
  1532. else {
  1533. /*
  1534. * This bool is made true when
  1535. * effective frequency >=
  1536. * target frequency is found
  1537. */
  1538. freq_found = true;
  1539. if ((rate /
  1540. (cpsdvsr * (1 + scr))) != freq) {
  1541. if (scr == SCR_MIN) {
  1542. cpsdvsr -= 2;
  1543. scr = SCR_MAX;
  1544. } else
  1545. scr -= 1;
  1546. }
  1547. }
  1548. }
  1549. if (!freq_found) {
  1550. cpsdvsr += 2;
  1551. scr = SCR_MIN;
  1552. }
  1553. }
  1554. if (cpsdvsr != 0) {
  1555. dev_dbg(&pl022->adev->dev,
  1556. "SSP Effective Frequency is %u\n",
  1557. (rate / (cpsdvsr * (1 + scr))));
  1558. clk_freq->cpsdvsr = (u8) (cpsdvsr & 0xFF);
  1559. clk_freq->scr = (u8) (scr & 0xFF);
  1560. dev_dbg(&pl022->adev->dev,
  1561. "SSP cpsdvsr = %d, scr = %d\n",
  1562. clk_freq->cpsdvsr, clk_freq->scr);
  1563. }
  1564. } else {
  1565. dev_err(&pl022->adev->dev,
  1566. "controller data is incorrect: out of range frequency");
  1567. return -EINVAL;
  1568. }
  1569. return 0;
  1570. }
  1571. /*
  1572. * A piece of default chip info unless the platform
  1573. * supplies it.
  1574. */
  1575. static const struct pl022_config_chip pl022_default_chip_info = {
  1576. .com_mode = POLLING_TRANSFER,
  1577. .iface = SSP_INTERFACE_MOTOROLA_SPI,
  1578. .hierarchy = SSP_SLAVE,
  1579. .slave_tx_disable = DO_NOT_DRIVE_TX,
  1580. .rx_lev_trig = SSP_RX_1_OR_MORE_ELEM,
  1581. .tx_lev_trig = SSP_TX_1_OR_MORE_EMPTY_LOC,
  1582. .ctrl_len = SSP_BITS_8,
  1583. .wait_state = SSP_MWIRE_WAIT_ZERO,
  1584. .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
  1585. .cs_control = null_cs_control,
  1586. };
  1587. /**
  1588. * pl022_setup - setup function registered to SPI master framework
  1589. * @spi: spi device which is requesting setup
  1590. *
  1591. * This function is registered to the SPI framework for this SPI master
  1592. * controller. If it is the first time when setup is called by this device,
  1593. * this function will initialize the runtime state for this chip and save
  1594. * the same in the device structure. Else it will update the runtime info
  1595. * with the updated chip info. Nothing is really being written to the
  1596. * controller hardware here, that is not done until the actual transfer
  1597. * commence.
  1598. */
  1599. static int pl022_setup(struct spi_device *spi)
  1600. {
  1601. struct pl022_config_chip const *chip_info;
  1602. struct chip_data *chip;
  1603. struct ssp_clock_params clk_freq = {0, };
  1604. int status = 0;
  1605. struct pl022 *pl022 = spi_master_get_devdata(spi->master);
  1606. unsigned int bits = spi->bits_per_word;
  1607. u32 tmp;
  1608. if (!spi->max_speed_hz)
  1609. return -EINVAL;
  1610. /* Get controller_state if one is supplied */
  1611. chip = spi_get_ctldata(spi);
  1612. if (chip == NULL) {
  1613. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1614. if (!chip) {
  1615. dev_err(&spi->dev,
  1616. "cannot allocate controller state\n");
  1617. return -ENOMEM;
  1618. }
  1619. dev_dbg(&spi->dev,
  1620. "allocated memory for controller's runtime state\n");
  1621. }
  1622. /* Get controller data if one is supplied */
  1623. chip_info = spi->controller_data;
  1624. if (chip_info == NULL) {
  1625. chip_info = &pl022_default_chip_info;
  1626. /* spi_board_info.controller_data not is supplied */
  1627. dev_dbg(&spi->dev,
  1628. "using default controller_data settings\n");
  1629. } else
  1630. dev_dbg(&spi->dev,
  1631. "using user supplied controller_data settings\n");
  1632. /*
  1633. * We can override with custom divisors, else we use the board
  1634. * frequency setting
  1635. */
  1636. if ((0 == chip_info->clk_freq.cpsdvsr)
  1637. && (0 == chip_info->clk_freq.scr)) {
  1638. status = calculate_effective_freq(pl022,
  1639. spi->max_speed_hz,
  1640. &clk_freq);
  1641. if (status < 0)
  1642. goto err_config_params;
  1643. } else {
  1644. memcpy(&clk_freq, &chip_info->clk_freq, sizeof(clk_freq));
  1645. if ((clk_freq.cpsdvsr % 2) != 0)
  1646. clk_freq.cpsdvsr =
  1647. clk_freq.cpsdvsr - 1;
  1648. }
  1649. if ((clk_freq.cpsdvsr < CPSDVR_MIN)
  1650. || (clk_freq.cpsdvsr > CPSDVR_MAX)) {
  1651. dev_err(&spi->dev,
  1652. "cpsdvsr is configured incorrectly\n");
  1653. goto err_config_params;
  1654. }
  1655. status = verify_controller_parameters(pl022, chip_info);
  1656. if (status) {
  1657. dev_err(&spi->dev, "controller data is incorrect");
  1658. goto err_config_params;
  1659. }
  1660. /* Now set controller state based on controller data */
  1661. chip->xfer_type = chip_info->com_mode;
  1662. if (!chip_info->cs_control) {
  1663. chip->cs_control = null_cs_control;
  1664. dev_warn(&spi->dev,
  1665. "chip select function is NULL for this chip\n");
  1666. } else
  1667. chip->cs_control = chip_info->cs_control;
  1668. if (bits <= 3) {
  1669. /* PL022 doesn't support less than 4-bits */
  1670. status = -ENOTSUPP;
  1671. goto err_config_params;
  1672. } else if (bits <= 8) {
  1673. dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
  1674. chip->n_bytes = 1;
  1675. chip->read = READING_U8;
  1676. chip->write = WRITING_U8;
  1677. } else if (bits <= 16) {
  1678. dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
  1679. chip->n_bytes = 2;
  1680. chip->read = READING_U16;
  1681. chip->write = WRITING_U16;
  1682. } else {
  1683. if (pl022->vendor->max_bpw >= 32) {
  1684. dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
  1685. chip->n_bytes = 4;
  1686. chip->read = READING_U32;
  1687. chip->write = WRITING_U32;
  1688. } else {
  1689. dev_err(&spi->dev,
  1690. "illegal data size for this controller!\n");
  1691. dev_err(&spi->dev,
  1692. "a standard pl022 can only handle "
  1693. "1 <= n <= 16 bit words\n");
  1694. status = -ENOTSUPP;
  1695. goto err_config_params;
  1696. }
  1697. }
  1698. /* Now Initialize all register settings required for this chip */
  1699. chip->cr0 = 0;
  1700. chip->cr1 = 0;
  1701. chip->dmacr = 0;
  1702. chip->cpsr = 0;
  1703. if ((chip_info->com_mode == DMA_TRANSFER)
  1704. && ((pl022->master_info)->enable_dma)) {
  1705. chip->enable_dma = true;
  1706. dev_dbg(&spi->dev, "DMA mode set in controller state\n");
  1707. if (status < 0)
  1708. goto err_config_params;
  1709. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1710. SSP_DMACR_MASK_RXDMAE, 0);
  1711. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_ENABLED,
  1712. SSP_DMACR_MASK_TXDMAE, 1);
  1713. } else {
  1714. chip->enable_dma = false;
  1715. dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
  1716. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1717. SSP_DMACR_MASK_RXDMAE, 0);
  1718. SSP_WRITE_BITS(chip->dmacr, SSP_DMA_DISABLED,
  1719. SSP_DMACR_MASK_TXDMAE, 1);
  1720. }
  1721. chip->cpsr = clk_freq.cpsdvsr;
  1722. /* Special setup for the ST micro extended control registers */
  1723. if (pl022->vendor->extended_cr) {
  1724. u32 etx;
  1725. if (pl022->vendor->pl023) {
  1726. /* These bits are only in the PL023 */
  1727. SSP_WRITE_BITS(chip->cr1, chip_info->clkdelay,
  1728. SSP_CR1_MASK_FBCLKDEL_ST, 13);
  1729. } else {
  1730. /* These bits are in the PL022 but not PL023 */
  1731. SSP_WRITE_BITS(chip->cr0, chip_info->duplex,
  1732. SSP_CR0_MASK_HALFDUP_ST, 5);
  1733. SSP_WRITE_BITS(chip->cr0, chip_info->ctrl_len,
  1734. SSP_CR0_MASK_CSS_ST, 16);
  1735. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1736. SSP_CR0_MASK_FRF_ST, 21);
  1737. SSP_WRITE_BITS(chip->cr1, chip_info->wait_state,
  1738. SSP_CR1_MASK_MWAIT_ST, 6);
  1739. }
  1740. SSP_WRITE_BITS(chip->cr0, bits - 1,
  1741. SSP_CR0_MASK_DSS_ST, 0);
  1742. if (spi->mode & SPI_LSB_FIRST) {
  1743. tmp = SSP_RX_LSB;
  1744. etx = SSP_TX_LSB;
  1745. } else {
  1746. tmp = SSP_RX_MSB;
  1747. etx = SSP_TX_MSB;
  1748. }
  1749. SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_RENDN_ST, 4);
  1750. SSP_WRITE_BITS(chip->cr1, etx, SSP_CR1_MASK_TENDN_ST, 5);
  1751. SSP_WRITE_BITS(chip->cr1, chip_info->rx_lev_trig,
  1752. SSP_CR1_MASK_RXIFLSEL_ST, 7);
  1753. SSP_WRITE_BITS(chip->cr1, chip_info->tx_lev_trig,
  1754. SSP_CR1_MASK_TXIFLSEL_ST, 10);
  1755. } else {
  1756. SSP_WRITE_BITS(chip->cr0, bits - 1,
  1757. SSP_CR0_MASK_DSS, 0);
  1758. SSP_WRITE_BITS(chip->cr0, chip_info->iface,
  1759. SSP_CR0_MASK_FRF, 4);
  1760. }
  1761. /* Stuff that is common for all versions */
  1762. if (spi->mode & SPI_CPOL)
  1763. tmp = SSP_CLK_POL_IDLE_HIGH;
  1764. else
  1765. tmp = SSP_CLK_POL_IDLE_LOW;
  1766. SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPO, 6);
  1767. if (spi->mode & SPI_CPHA)
  1768. tmp = SSP_CLK_SECOND_EDGE;
  1769. else
  1770. tmp = SSP_CLK_FIRST_EDGE;
  1771. SSP_WRITE_BITS(chip->cr0, tmp, SSP_CR0_MASK_SPH, 7);
  1772. SSP_WRITE_BITS(chip->cr0, clk_freq.scr, SSP_CR0_MASK_SCR, 8);
  1773. /* Loopback is available on all versions except PL023 */
  1774. if (!pl022->vendor->pl023) {
  1775. if (spi->mode & SPI_LOOP)
  1776. tmp = LOOPBACK_ENABLED;
  1777. else
  1778. tmp = LOOPBACK_DISABLED;
  1779. SSP_WRITE_BITS(chip->cr1, tmp, SSP_CR1_MASK_LBM, 0);
  1780. }
  1781. SSP_WRITE_BITS(chip->cr1, SSP_DISABLED, SSP_CR1_MASK_SSE, 1);
  1782. SSP_WRITE_BITS(chip->cr1, chip_info->hierarchy, SSP_CR1_MASK_MS, 2);
  1783. SSP_WRITE_BITS(chip->cr1, chip_info->slave_tx_disable, SSP_CR1_MASK_SOD, 3);
  1784. /* Save controller_state */
  1785. spi_set_ctldata(spi, chip);
  1786. return status;
  1787. err_config_params:
  1788. spi_set_ctldata(spi, NULL);
  1789. kfree(chip);
  1790. return status;
  1791. }
  1792. /**
  1793. * pl022_cleanup - cleanup function registered to SPI master framework
  1794. * @spi: spi device which is requesting cleanup
  1795. *
  1796. * This function is registered to the SPI framework for this SPI master
  1797. * controller. It will free the runtime state of chip.
  1798. */
  1799. static void pl022_cleanup(struct spi_device *spi)
  1800. {
  1801. struct chip_data *chip = spi_get_ctldata(spi);
  1802. spi_set_ctldata(spi, NULL);
  1803. kfree(chip);
  1804. }
  1805. static int __devinit
  1806. pl022_probe(struct amba_device *adev, struct amba_id *id)
  1807. {
  1808. struct device *dev = &adev->dev;
  1809. struct pl022_ssp_controller *platform_info = adev->dev.platform_data;
  1810. struct spi_master *master;
  1811. struct pl022 *pl022 = NULL; /*Data for this driver */
  1812. int status = 0;
  1813. dev_info(&adev->dev,
  1814. "ARM PL022 driver, device ID: 0x%08x\n", adev->periphid);
  1815. if (platform_info == NULL) {
  1816. dev_err(&adev->dev, "probe - no platform data supplied\n");
  1817. status = -ENODEV;
  1818. goto err_no_pdata;
  1819. }
  1820. /* Allocate master with space for data */
  1821. master = spi_alloc_master(dev, sizeof(struct pl022));
  1822. if (master == NULL) {
  1823. dev_err(&adev->dev, "probe - cannot alloc SPI master\n");
  1824. status = -ENOMEM;
  1825. goto err_no_master;
  1826. }
  1827. pl022 = spi_master_get_devdata(master);
  1828. pl022->master = master;
  1829. pl022->master_info = platform_info;
  1830. pl022->adev = adev;
  1831. pl022->vendor = id->data;
  1832. /*
  1833. * Bus Number Which has been Assigned to this SSP controller
  1834. * on this board
  1835. */
  1836. master->bus_num = platform_info->bus_id;
  1837. master->num_chipselect = platform_info->num_chipselect;
  1838. master->cleanup = pl022_cleanup;
  1839. master->setup = pl022_setup;
  1840. master->transfer = pl022_transfer;
  1841. /*
  1842. * Supports mode 0-3, loopback, and active low CS. Transfers are
  1843. * always MS bit first on the original pl022.
  1844. */
  1845. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1846. if (pl022->vendor->extended_cr)
  1847. master->mode_bits |= SPI_LSB_FIRST;
  1848. dev_dbg(&adev->dev, "BUSNO: %d\n", master->bus_num);
  1849. status = amba_request_regions(adev, NULL);
  1850. if (status)
  1851. goto err_no_ioregion;
  1852. pl022->phybase = adev->res.start;
  1853. pl022->virtbase = ioremap(adev->res.start, resource_size(&adev->res));
  1854. if (pl022->virtbase == NULL) {
  1855. status = -ENOMEM;
  1856. goto err_no_ioremap;
  1857. }
  1858. printk(KERN_INFO "pl022: mapped registers from 0x%08x to %p\n",
  1859. adev->res.start, pl022->virtbase);
  1860. pl022->clk = clk_get(&adev->dev, NULL);
  1861. if (IS_ERR(pl022->clk)) {
  1862. status = PTR_ERR(pl022->clk);
  1863. dev_err(&adev->dev, "could not retrieve SSP/SPI bus clock\n");
  1864. goto err_no_clk;
  1865. }
  1866. /* Disable SSP */
  1867. writew((readw(SSP_CR1(pl022->virtbase)) & (~SSP_CR1_MASK_SSE)),
  1868. SSP_CR1(pl022->virtbase));
  1869. load_ssp_default_config(pl022);
  1870. status = request_irq(adev->irq[0], pl022_interrupt_handler, 0, "pl022",
  1871. pl022);
  1872. if (status < 0) {
  1873. dev_err(&adev->dev, "probe - cannot get IRQ (%d)\n", status);
  1874. goto err_no_irq;
  1875. }
  1876. /* Get DMA channels */
  1877. if (platform_info->enable_dma) {
  1878. status = pl022_dma_probe(pl022);
  1879. if (status != 0)
  1880. goto err_no_dma;
  1881. }
  1882. /* Initialize and start queue */
  1883. status = init_queue(pl022);
  1884. if (status != 0) {
  1885. dev_err(&adev->dev, "probe - problem initializing queue\n");
  1886. goto err_init_queue;
  1887. }
  1888. status = start_queue(pl022);
  1889. if (status != 0) {
  1890. dev_err(&adev->dev, "probe - problem starting queue\n");
  1891. goto err_start_queue;
  1892. }
  1893. /* Register with the SPI framework */
  1894. amba_set_drvdata(adev, pl022);
  1895. status = spi_register_master(master);
  1896. if (status != 0) {
  1897. dev_err(&adev->dev,
  1898. "probe - problem registering spi master\n");
  1899. goto err_spi_register;
  1900. }
  1901. dev_dbg(dev, "probe succeded\n");
  1902. /* Disable the silicon block pclk and clock it when needed */
  1903. amba_pclk_disable(adev);
  1904. return 0;
  1905. err_spi_register:
  1906. err_start_queue:
  1907. err_init_queue:
  1908. destroy_queue(pl022);
  1909. pl022_dma_remove(pl022);
  1910. err_no_dma:
  1911. free_irq(adev->irq[0], pl022);
  1912. err_no_irq:
  1913. clk_put(pl022->clk);
  1914. err_no_clk:
  1915. iounmap(pl022->virtbase);
  1916. err_no_ioremap:
  1917. amba_release_regions(adev);
  1918. err_no_ioregion:
  1919. spi_master_put(master);
  1920. err_no_master:
  1921. err_no_pdata:
  1922. return status;
  1923. }
  1924. static int __devexit
  1925. pl022_remove(struct amba_device *adev)
  1926. {
  1927. struct pl022 *pl022 = amba_get_drvdata(adev);
  1928. int status = 0;
  1929. if (!pl022)
  1930. return 0;
  1931. /* Remove the queue */
  1932. status = destroy_queue(pl022);
  1933. if (status != 0) {
  1934. dev_err(&adev->dev,
  1935. "queue remove failed (%d)\n", status);
  1936. return status;
  1937. }
  1938. load_ssp_default_config(pl022);
  1939. pl022_dma_remove(pl022);
  1940. free_irq(adev->irq[0], pl022);
  1941. clk_disable(pl022->clk);
  1942. clk_put(pl022->clk);
  1943. iounmap(pl022->virtbase);
  1944. amba_release_regions(adev);
  1945. tasklet_disable(&pl022->pump_transfers);
  1946. spi_unregister_master(pl022->master);
  1947. spi_master_put(pl022->master);
  1948. amba_set_drvdata(adev, NULL);
  1949. dev_dbg(&adev->dev, "remove succeded\n");
  1950. return 0;
  1951. }
  1952. #ifdef CONFIG_PM
  1953. static int pl022_suspend(struct amba_device *adev, pm_message_t state)
  1954. {
  1955. struct pl022 *pl022 = amba_get_drvdata(adev);
  1956. int status = 0;
  1957. status = stop_queue(pl022);
  1958. if (status) {
  1959. dev_warn(&adev->dev, "suspend cannot stop queue\n");
  1960. return status;
  1961. }
  1962. amba_pclk_enable(adev);
  1963. load_ssp_default_config(pl022);
  1964. amba_pclk_disable(adev);
  1965. dev_dbg(&adev->dev, "suspended\n");
  1966. return 0;
  1967. }
  1968. static int pl022_resume(struct amba_device *adev)
  1969. {
  1970. struct pl022 *pl022 = amba_get_drvdata(adev);
  1971. int status = 0;
  1972. /* Start the queue running */
  1973. status = start_queue(pl022);
  1974. if (status)
  1975. dev_err(&adev->dev, "problem starting queue (%d)\n", status);
  1976. else
  1977. dev_dbg(&adev->dev, "resumed\n");
  1978. return status;
  1979. }
  1980. #else
  1981. #define pl022_suspend NULL
  1982. #define pl022_resume NULL
  1983. #endif /* CONFIG_PM */
  1984. static struct vendor_data vendor_arm = {
  1985. .fifodepth = 8,
  1986. .max_bpw = 16,
  1987. .unidir = false,
  1988. .extended_cr = false,
  1989. .pl023 = false,
  1990. };
  1991. static struct vendor_data vendor_st = {
  1992. .fifodepth = 32,
  1993. .max_bpw = 32,
  1994. .unidir = false,
  1995. .extended_cr = true,
  1996. .pl023 = false,
  1997. };
  1998. static struct vendor_data vendor_st_pl023 = {
  1999. .fifodepth = 32,
  2000. .max_bpw = 32,
  2001. .unidir = false,
  2002. .extended_cr = true,
  2003. .pl023 = true,
  2004. };
  2005. static struct amba_id pl022_ids[] = {
  2006. {
  2007. /*
  2008. * ARM PL022 variant, this has a 16bit wide
  2009. * and 8 locations deep TX/RX FIFO
  2010. */
  2011. .id = 0x00041022,
  2012. .mask = 0x000fffff,
  2013. .data = &vendor_arm,
  2014. },
  2015. {
  2016. /*
  2017. * ST Micro derivative, this has 32bit wide
  2018. * and 32 locations deep TX/RX FIFO
  2019. */
  2020. .id = 0x01080022,
  2021. .mask = 0xffffffff,
  2022. .data = &vendor_st,
  2023. },
  2024. {
  2025. /*
  2026. * ST-Ericsson derivative "PL023" (this is not
  2027. * an official ARM number), this is a PL022 SSP block
  2028. * stripped to SPI mode only, it has 32bit wide
  2029. * and 32 locations deep TX/RX FIFO but no extended
  2030. * CR0/CR1 register
  2031. */
  2032. .id = 0x00080023,
  2033. .mask = 0xffffffff,
  2034. .data = &vendor_st_pl023,
  2035. },
  2036. { 0, 0 },
  2037. };
  2038. static struct amba_driver pl022_driver = {
  2039. .drv = {
  2040. .name = "ssp-pl022",
  2041. },
  2042. .id_table = pl022_ids,
  2043. .probe = pl022_probe,
  2044. .remove = __devexit_p(pl022_remove),
  2045. .suspend = pl022_suspend,
  2046. .resume = pl022_resume,
  2047. };
  2048. static int __init pl022_init(void)
  2049. {
  2050. return amba_driver_register(&pl022_driver);
  2051. }
  2052. subsys_initcall(pl022_init);
  2053. static void __exit pl022_exit(void)
  2054. {
  2055. amba_driver_unregister(&pl022_driver);
  2056. }
  2057. module_exit(pl022_exit);
  2058. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  2059. MODULE_DESCRIPTION("PL022 SSP Controller Driver");
  2060. MODULE_LICENSE("GPL");