fw-ohci.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510
  1. /*
  2. * Driver for OHCI 1394 controllers
  3. *
  4. * Copyright (C) 2003-2006 Kristian Hoegsberg <krh@bitplanet.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/compiler.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/gfp.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/pci.h>
  31. #include <linux/spinlock.h>
  32. #include <asm/page.h>
  33. #include <asm/system.h>
  34. #ifdef CONFIG_PPC_PMAC
  35. #include <asm/pmac_feature.h>
  36. #endif
  37. #include "fw-ohci.h"
  38. #include "fw-transaction.h"
  39. #define DESCRIPTOR_OUTPUT_MORE 0
  40. #define DESCRIPTOR_OUTPUT_LAST (1 << 12)
  41. #define DESCRIPTOR_INPUT_MORE (2 << 12)
  42. #define DESCRIPTOR_INPUT_LAST (3 << 12)
  43. #define DESCRIPTOR_STATUS (1 << 11)
  44. #define DESCRIPTOR_KEY_IMMEDIATE (2 << 8)
  45. #define DESCRIPTOR_PING (1 << 7)
  46. #define DESCRIPTOR_YY (1 << 6)
  47. #define DESCRIPTOR_NO_IRQ (0 << 4)
  48. #define DESCRIPTOR_IRQ_ERROR (1 << 4)
  49. #define DESCRIPTOR_IRQ_ALWAYS (3 << 4)
  50. #define DESCRIPTOR_BRANCH_ALWAYS (3 << 2)
  51. #define DESCRIPTOR_WAIT (3 << 0)
  52. struct descriptor {
  53. __le16 req_count;
  54. __le16 control;
  55. __le32 data_address;
  56. __le32 branch_address;
  57. __le16 res_count;
  58. __le16 transfer_status;
  59. } __attribute__((aligned(16)));
  60. struct db_descriptor {
  61. __le16 first_size;
  62. __le16 control;
  63. __le16 second_req_count;
  64. __le16 first_req_count;
  65. __le32 branch_address;
  66. __le16 second_res_count;
  67. __le16 first_res_count;
  68. __le32 reserved0;
  69. __le32 first_buffer;
  70. __le32 second_buffer;
  71. __le32 reserved1;
  72. } __attribute__((aligned(16)));
  73. #define CONTROL_SET(regs) (regs)
  74. #define CONTROL_CLEAR(regs) ((regs) + 4)
  75. #define COMMAND_PTR(regs) ((regs) + 12)
  76. #define CONTEXT_MATCH(regs) ((regs) + 16)
  77. struct ar_buffer {
  78. struct descriptor descriptor;
  79. struct ar_buffer *next;
  80. __le32 data[0];
  81. };
  82. struct ar_context {
  83. struct fw_ohci *ohci;
  84. struct ar_buffer *current_buffer;
  85. struct ar_buffer *last_buffer;
  86. void *pointer;
  87. u32 regs;
  88. struct tasklet_struct tasklet;
  89. };
  90. struct context;
  91. typedef int (*descriptor_callback_t)(struct context *ctx,
  92. struct descriptor *d,
  93. struct descriptor *last);
  94. /*
  95. * A buffer that contains a block of DMA-able coherent memory used for
  96. * storing a portion of a DMA descriptor program.
  97. */
  98. struct descriptor_buffer {
  99. struct list_head list;
  100. dma_addr_t buffer_bus;
  101. size_t buffer_size;
  102. size_t used;
  103. struct descriptor buffer[0];
  104. };
  105. struct context {
  106. struct fw_ohci *ohci;
  107. u32 regs;
  108. int total_allocation;
  109. /*
  110. * List of page-sized buffers for storing DMA descriptors.
  111. * Head of list contains buffers in use and tail of list contains
  112. * free buffers.
  113. */
  114. struct list_head buffer_list;
  115. /*
  116. * Pointer to a buffer inside buffer_list that contains the tail
  117. * end of the current DMA program.
  118. */
  119. struct descriptor_buffer *buffer_tail;
  120. /*
  121. * The descriptor containing the branch address of the first
  122. * descriptor that has not yet been filled by the device.
  123. */
  124. struct descriptor *last;
  125. /*
  126. * The last descriptor in the DMA program. It contains the branch
  127. * address that must be updated upon appending a new descriptor.
  128. */
  129. struct descriptor *prev;
  130. descriptor_callback_t callback;
  131. struct tasklet_struct tasklet;
  132. };
  133. #define IT_HEADER_SY(v) ((v) << 0)
  134. #define IT_HEADER_TCODE(v) ((v) << 4)
  135. #define IT_HEADER_CHANNEL(v) ((v) << 8)
  136. #define IT_HEADER_TAG(v) ((v) << 14)
  137. #define IT_HEADER_SPEED(v) ((v) << 16)
  138. #define IT_HEADER_DATA_LENGTH(v) ((v) << 16)
  139. struct iso_context {
  140. struct fw_iso_context base;
  141. struct context context;
  142. int excess_bytes;
  143. void *header;
  144. size_t header_length;
  145. };
  146. #define CONFIG_ROM_SIZE 1024
  147. struct fw_ohci {
  148. struct fw_card card;
  149. u32 version;
  150. __iomem char *registers;
  151. dma_addr_t self_id_bus;
  152. __le32 *self_id_cpu;
  153. struct tasklet_struct bus_reset_tasklet;
  154. int node_id;
  155. int generation;
  156. int request_generation;
  157. u32 bus_seconds;
  158. bool old_uninorth;
  159. /*
  160. * Spinlock for accessing fw_ohci data. Never call out of
  161. * this driver with this lock held.
  162. */
  163. spinlock_t lock;
  164. u32 self_id_buffer[512];
  165. /* Config rom buffers */
  166. __be32 *config_rom;
  167. dma_addr_t config_rom_bus;
  168. __be32 *next_config_rom;
  169. dma_addr_t next_config_rom_bus;
  170. u32 next_header;
  171. struct ar_context ar_request_ctx;
  172. struct ar_context ar_response_ctx;
  173. struct context at_request_ctx;
  174. struct context at_response_ctx;
  175. u32 it_context_mask;
  176. struct iso_context *it_context_list;
  177. u32 ir_context_mask;
  178. struct iso_context *ir_context_list;
  179. };
  180. static inline struct fw_ohci *fw_ohci(struct fw_card *card)
  181. {
  182. return container_of(card, struct fw_ohci, card);
  183. }
  184. #define IT_CONTEXT_CYCLE_MATCH_ENABLE 0x80000000
  185. #define IR_CONTEXT_BUFFER_FILL 0x80000000
  186. #define IR_CONTEXT_ISOCH_HEADER 0x40000000
  187. #define IR_CONTEXT_CYCLE_MATCH_ENABLE 0x20000000
  188. #define IR_CONTEXT_MULTI_CHANNEL_MODE 0x10000000
  189. #define IR_CONTEXT_DUAL_BUFFER_MODE 0x08000000
  190. #define CONTEXT_RUN 0x8000
  191. #define CONTEXT_WAKE 0x1000
  192. #define CONTEXT_DEAD 0x0800
  193. #define CONTEXT_ACTIVE 0x0400
  194. #define OHCI1394_MAX_AT_REQ_RETRIES 0x2
  195. #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
  196. #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
  197. #define FW_OHCI_MAJOR 240
  198. #define OHCI1394_REGISTER_SIZE 0x800
  199. #define OHCI_LOOP_COUNT 500
  200. #define OHCI1394_PCI_HCI_Control 0x40
  201. #define SELF_ID_BUF_SIZE 0x800
  202. #define OHCI_TCODE_PHY_PACKET 0x0e
  203. #define OHCI_VERSION_1_1 0x010010
  204. static char ohci_driver_name[] = KBUILD_MODNAME;
  205. #ifdef CONFIG_FIREWIRE_OHCI_DEBUG
  206. #define OHCI_PARAM_DEBUG_IRQS 1
  207. #define OHCI_PARAM_DEBUG_SELFIDS 2
  208. #define OHCI_PARAM_DEBUG_AT_AR 4
  209. static int param_debug;
  210. module_param_named(debug, param_debug, int, 0644);
  211. MODULE_PARM_DESC(debug, "Verbose logging (default = 0"
  212. ", IRQs = " __stringify(OHCI_PARAM_DEBUG_IRQS)
  213. ", self-IDs = " __stringify(OHCI_PARAM_DEBUG_SELFIDS)
  214. ", AT/AR events = " __stringify(OHCI_PARAM_DEBUG_AT_AR)
  215. ", or a combination, or all = -1)");
  216. static void log_irqs(u32 evt)
  217. {
  218. if (likely(!(param_debug & OHCI_PARAM_DEBUG_IRQS)))
  219. return;
  220. printk(KERN_DEBUG KBUILD_MODNAME ": IRQ %08x%s%s%s%s%s%s%s%s%s%s%s\n",
  221. evt,
  222. evt & OHCI1394_selfIDComplete ? " selfID" : "",
  223. evt & OHCI1394_RQPkt ? " AR_req" : "",
  224. evt & OHCI1394_RSPkt ? " AR_resp" : "",
  225. evt & OHCI1394_reqTxComplete ? " AT_req" : "",
  226. evt & OHCI1394_respTxComplete ? " AT_resp" : "",
  227. evt & OHCI1394_isochRx ? " IR" : "",
  228. evt & OHCI1394_isochTx ? " IT" : "",
  229. evt & OHCI1394_postedWriteErr ? " postedWriteErr" : "",
  230. evt & OHCI1394_cycleTooLong ? " cycleTooLong" : "",
  231. evt & OHCI1394_cycle64Seconds ? " cycle64Seconds" : "",
  232. evt & ~(OHCI1394_selfIDComplete | OHCI1394_RQPkt |
  233. OHCI1394_RSPkt | OHCI1394_reqTxComplete |
  234. OHCI1394_respTxComplete | OHCI1394_isochRx |
  235. OHCI1394_isochTx | OHCI1394_postedWriteErr |
  236. OHCI1394_cycleTooLong | OHCI1394_cycle64Seconds)
  237. ? " ?" : "");
  238. }
  239. static const char *speed[] = {
  240. [0] = "S100", [1] = "S200", [2] = "S400", [3] = "beta",
  241. };
  242. static const char *power[] = {
  243. [0] = "+0W", [1] = "+15W", [2] = "+30W", [3] = "+45W",
  244. [4] = "-3W", [5] = " ?W", [6] = "-3..-6W", [7] = "-3..-10W",
  245. };
  246. static const char port[] = { '.', '-', 'p', 'c', };
  247. static char _p(u32 *s, int shift)
  248. {
  249. return port[*s >> shift & 3];
  250. }
  251. static void log_selfids(int generation, int self_id_count, u32 *s)
  252. {
  253. if (likely(!(param_debug & OHCI_PARAM_DEBUG_SELFIDS)))
  254. return;
  255. printk(KERN_DEBUG KBUILD_MODNAME ": %d selfIDs, generation %d\n",
  256. self_id_count, generation);
  257. for (; self_id_count--; ++s)
  258. if ((*s & 1 << 23) == 0)
  259. printk(KERN_DEBUG "selfID 0: %08x, phy %d [%c%c%c] "
  260. "%s gc=%d %s %s%s%s\n",
  261. *s, *s >> 24 & 63, _p(s, 6), _p(s, 4), _p(s, 2),
  262. speed[*s >> 14 & 3], *s >> 16 & 63,
  263. power[*s >> 8 & 7], *s >> 22 & 1 ? "L" : "",
  264. *s >> 11 & 1 ? "c" : "", *s & 2 ? "i" : "");
  265. else
  266. printk(KERN_DEBUG "selfID n: %08x, phy %d "
  267. "[%c%c%c%c%c%c%c%c]\n",
  268. *s, *s >> 24 & 63,
  269. _p(s, 16), _p(s, 14), _p(s, 12), _p(s, 10),
  270. _p(s, 8), _p(s, 6), _p(s, 4), _p(s, 2));
  271. }
  272. static const char *evts[] = {
  273. [0x00] = "evt_no_status", [0x01] = "-reserved-",
  274. [0x02] = "evt_long_packet", [0x03] = "evt_missing_ack",
  275. [0x04] = "evt_underrun", [0x05] = "evt_overrun",
  276. [0x06] = "evt_descriptor_read", [0x07] = "evt_data_read",
  277. [0x08] = "evt_data_write", [0x09] = "evt_bus_reset",
  278. [0x0a] = "evt_timeout", [0x0b] = "evt_tcode_err",
  279. [0x0c] = "-reserved-", [0x0d] = "-reserved-",
  280. [0x0e] = "evt_unknown", [0x0f] = "evt_flushed",
  281. [0x10] = "-reserved-", [0x11] = "ack_complete",
  282. [0x12] = "ack_pending ", [0x13] = "-reserved-",
  283. [0x14] = "ack_busy_X", [0x15] = "ack_busy_A",
  284. [0x16] = "ack_busy_B", [0x17] = "-reserved-",
  285. [0x18] = "-reserved-", [0x19] = "-reserved-",
  286. [0x1a] = "-reserved-", [0x1b] = "ack_tardy",
  287. [0x1c] = "-reserved-", [0x1d] = "ack_data_error",
  288. [0x1e] = "ack_type_error", [0x1f] = "-reserved-",
  289. [0x20] = "pending/cancelled",
  290. };
  291. static const char *tcodes[] = {
  292. [0x0] = "QW req", [0x1] = "BW req",
  293. [0x2] = "W resp", [0x3] = "-reserved-",
  294. [0x4] = "QR req", [0x5] = "BR req",
  295. [0x6] = "QR resp", [0x7] = "BR resp",
  296. [0x8] = "cycle start", [0x9] = "Lk req",
  297. [0xa] = "async stream packet", [0xb] = "Lk resp",
  298. [0xc] = "-reserved-", [0xd] = "-reserved-",
  299. [0xe] = "link internal", [0xf] = "-reserved-",
  300. };
  301. static const char *phys[] = {
  302. [0x0] = "phy config packet", [0x1] = "link-on packet",
  303. [0x2] = "self-id packet", [0x3] = "-reserved-",
  304. };
  305. static void log_ar_at_event(char dir, int speed, u32 *header, int evt)
  306. {
  307. int tcode = header[0] >> 4 & 0xf;
  308. char specific[12];
  309. if (likely(!(param_debug & OHCI_PARAM_DEBUG_AT_AR)))
  310. return;
  311. if (unlikely(evt >= ARRAY_SIZE(evts)))
  312. evt = 0x1f;
  313. if (header[0] == ~header[1]) {
  314. printk(KERN_DEBUG "A%c %s, %s, %08x\n",
  315. dir, evts[evt], phys[header[0] >> 30 & 0x3],
  316. header[0]);
  317. return;
  318. }
  319. switch (tcode) {
  320. case 0x0: case 0x6: case 0x8:
  321. snprintf(specific, sizeof(specific), " = %08x",
  322. be32_to_cpu((__force __be32)header[3]));
  323. break;
  324. case 0x1: case 0x5: case 0x7: case 0x9: case 0xb:
  325. snprintf(specific, sizeof(specific), " %x,%x",
  326. header[3] >> 16, header[3] & 0xffff);
  327. break;
  328. default:
  329. specific[0] = '\0';
  330. }
  331. switch (tcode) {
  332. case 0xe: case 0xa:
  333. printk(KERN_DEBUG "A%c %s, %s\n",
  334. dir, evts[evt], tcodes[tcode]);
  335. break;
  336. case 0x0: case 0x1: case 0x4: case 0x5: case 0x9:
  337. printk(KERN_DEBUG "A%c spd %x tl %02x, "
  338. "%04x -> %04x, %s, "
  339. "%s, %04x%08x%s\n",
  340. dir, speed, header[0] >> 10 & 0x3f,
  341. header[1] >> 16, header[0] >> 16, evts[evt],
  342. tcodes[tcode], header[1] & 0xffff, header[2], specific);
  343. break;
  344. default:
  345. printk(KERN_DEBUG "A%c spd %x tl %02x, "
  346. "%04x -> %04x, %s, "
  347. "%s%s\n",
  348. dir, speed, header[0] >> 10 & 0x3f,
  349. header[1] >> 16, header[0] >> 16, evts[evt],
  350. tcodes[tcode], specific);
  351. }
  352. }
  353. #else
  354. #define log_irqs(evt)
  355. #define log_selfids(generation, self_id_count, sid)
  356. #define log_ar_at_event(dir, speed, header, evt)
  357. #endif /* CONFIG_FIREWIRE_OHCI_DEBUG */
  358. static inline void reg_write(const struct fw_ohci *ohci, int offset, u32 data)
  359. {
  360. writel(data, ohci->registers + offset);
  361. }
  362. static inline u32 reg_read(const struct fw_ohci *ohci, int offset)
  363. {
  364. return readl(ohci->registers + offset);
  365. }
  366. static inline void flush_writes(const struct fw_ohci *ohci)
  367. {
  368. /* Do a dummy read to flush writes. */
  369. reg_read(ohci, OHCI1394_Version);
  370. }
  371. static int
  372. ohci_update_phy_reg(struct fw_card *card, int addr,
  373. int clear_bits, int set_bits)
  374. {
  375. struct fw_ohci *ohci = fw_ohci(card);
  376. u32 val, old;
  377. reg_write(ohci, OHCI1394_PhyControl, OHCI1394_PhyControl_Read(addr));
  378. flush_writes(ohci);
  379. msleep(2);
  380. val = reg_read(ohci, OHCI1394_PhyControl);
  381. if ((val & OHCI1394_PhyControl_ReadDone) == 0) {
  382. fw_error("failed to set phy reg bits.\n");
  383. return -EBUSY;
  384. }
  385. old = OHCI1394_PhyControl_ReadData(val);
  386. old = (old & ~clear_bits) | set_bits;
  387. reg_write(ohci, OHCI1394_PhyControl,
  388. OHCI1394_PhyControl_Write(addr, old));
  389. return 0;
  390. }
  391. static int ar_context_add_page(struct ar_context *ctx)
  392. {
  393. struct device *dev = ctx->ohci->card.device;
  394. struct ar_buffer *ab;
  395. dma_addr_t uninitialized_var(ab_bus);
  396. size_t offset;
  397. ab = dma_alloc_coherent(dev, PAGE_SIZE, &ab_bus, GFP_ATOMIC);
  398. if (ab == NULL)
  399. return -ENOMEM;
  400. memset(&ab->descriptor, 0, sizeof(ab->descriptor));
  401. ab->descriptor.control = cpu_to_le16(DESCRIPTOR_INPUT_MORE |
  402. DESCRIPTOR_STATUS |
  403. DESCRIPTOR_BRANCH_ALWAYS);
  404. offset = offsetof(struct ar_buffer, data);
  405. ab->descriptor.req_count = cpu_to_le16(PAGE_SIZE - offset);
  406. ab->descriptor.data_address = cpu_to_le32(ab_bus + offset);
  407. ab->descriptor.res_count = cpu_to_le16(PAGE_SIZE - offset);
  408. ab->descriptor.branch_address = 0;
  409. ctx->last_buffer->descriptor.branch_address = cpu_to_le32(ab_bus | 1);
  410. ctx->last_buffer->next = ab;
  411. ctx->last_buffer = ab;
  412. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  413. flush_writes(ctx->ohci);
  414. return 0;
  415. }
  416. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  417. #define cond_le32_to_cpu(v) \
  418. (ohci->old_uninorth ? (__force __u32)(v) : le32_to_cpu(v))
  419. #else
  420. #define cond_le32_to_cpu(v) le32_to_cpu(v)
  421. #endif
  422. static __le32 *handle_ar_packet(struct ar_context *ctx, __le32 *buffer)
  423. {
  424. struct fw_ohci *ohci = ctx->ohci;
  425. struct fw_packet p;
  426. u32 status, length, tcode;
  427. int evt;
  428. p.header[0] = cond_le32_to_cpu(buffer[0]);
  429. p.header[1] = cond_le32_to_cpu(buffer[1]);
  430. p.header[2] = cond_le32_to_cpu(buffer[2]);
  431. tcode = (p.header[0] >> 4) & 0x0f;
  432. switch (tcode) {
  433. case TCODE_WRITE_QUADLET_REQUEST:
  434. case TCODE_READ_QUADLET_RESPONSE:
  435. p.header[3] = (__force __u32) buffer[3];
  436. p.header_length = 16;
  437. p.payload_length = 0;
  438. break;
  439. case TCODE_READ_BLOCK_REQUEST :
  440. p.header[3] = cond_le32_to_cpu(buffer[3]);
  441. p.header_length = 16;
  442. p.payload_length = 0;
  443. break;
  444. case TCODE_WRITE_BLOCK_REQUEST:
  445. case TCODE_READ_BLOCK_RESPONSE:
  446. case TCODE_LOCK_REQUEST:
  447. case TCODE_LOCK_RESPONSE:
  448. p.header[3] = cond_le32_to_cpu(buffer[3]);
  449. p.header_length = 16;
  450. p.payload_length = p.header[3] >> 16;
  451. break;
  452. case TCODE_WRITE_RESPONSE:
  453. case TCODE_READ_QUADLET_REQUEST:
  454. case OHCI_TCODE_PHY_PACKET:
  455. p.header_length = 12;
  456. p.payload_length = 0;
  457. break;
  458. }
  459. p.payload = (void *) buffer + p.header_length;
  460. /* FIXME: What to do about evt_* errors? */
  461. length = (p.header_length + p.payload_length + 3) / 4;
  462. status = cond_le32_to_cpu(buffer[length]);
  463. evt = (status >> 16) & 0x1f;
  464. p.ack = evt - 16;
  465. p.speed = (status >> 21) & 0x7;
  466. p.timestamp = status & 0xffff;
  467. p.generation = ohci->request_generation;
  468. log_ar_at_event('R', p.speed, p.header, evt);
  469. /*
  470. * The OHCI bus reset handler synthesizes a phy packet with
  471. * the new generation number when a bus reset happens (see
  472. * section 8.4.2.3). This helps us determine when a request
  473. * was received and make sure we send the response in the same
  474. * generation. We only need this for requests; for responses
  475. * we use the unique tlabel for finding the matching
  476. * request.
  477. */
  478. if (evt == OHCI1394_evt_bus_reset)
  479. ohci->request_generation = (p.header[2] >> 16) & 0xff;
  480. else if (ctx == &ohci->ar_request_ctx)
  481. fw_core_handle_request(&ohci->card, &p);
  482. else
  483. fw_core_handle_response(&ohci->card, &p);
  484. return buffer + length + 1;
  485. }
  486. static void ar_context_tasklet(unsigned long data)
  487. {
  488. struct ar_context *ctx = (struct ar_context *)data;
  489. struct fw_ohci *ohci = ctx->ohci;
  490. struct ar_buffer *ab;
  491. struct descriptor *d;
  492. void *buffer, *end;
  493. ab = ctx->current_buffer;
  494. d = &ab->descriptor;
  495. if (d->res_count == 0) {
  496. size_t size, rest, offset;
  497. dma_addr_t start_bus;
  498. void *start;
  499. /*
  500. * This descriptor is finished and we may have a
  501. * packet split across this and the next buffer. We
  502. * reuse the page for reassembling the split packet.
  503. */
  504. offset = offsetof(struct ar_buffer, data);
  505. start = buffer = ab;
  506. start_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
  507. ab = ab->next;
  508. d = &ab->descriptor;
  509. size = buffer + PAGE_SIZE - ctx->pointer;
  510. rest = le16_to_cpu(d->req_count) - le16_to_cpu(d->res_count);
  511. memmove(buffer, ctx->pointer, size);
  512. memcpy(buffer + size, ab->data, rest);
  513. ctx->current_buffer = ab;
  514. ctx->pointer = (void *) ab->data + rest;
  515. end = buffer + size + rest;
  516. while (buffer < end)
  517. buffer = handle_ar_packet(ctx, buffer);
  518. dma_free_coherent(ohci->card.device, PAGE_SIZE,
  519. start, start_bus);
  520. ar_context_add_page(ctx);
  521. } else {
  522. buffer = ctx->pointer;
  523. ctx->pointer = end =
  524. (void *) ab + PAGE_SIZE - le16_to_cpu(d->res_count);
  525. while (buffer < end)
  526. buffer = handle_ar_packet(ctx, buffer);
  527. }
  528. }
  529. static int
  530. ar_context_init(struct ar_context *ctx, struct fw_ohci *ohci, u32 regs)
  531. {
  532. struct ar_buffer ab;
  533. ctx->regs = regs;
  534. ctx->ohci = ohci;
  535. ctx->last_buffer = &ab;
  536. tasklet_init(&ctx->tasklet, ar_context_tasklet, (unsigned long)ctx);
  537. ar_context_add_page(ctx);
  538. ar_context_add_page(ctx);
  539. ctx->current_buffer = ab.next;
  540. ctx->pointer = ctx->current_buffer->data;
  541. return 0;
  542. }
  543. static void ar_context_run(struct ar_context *ctx)
  544. {
  545. struct ar_buffer *ab = ctx->current_buffer;
  546. dma_addr_t ab_bus;
  547. size_t offset;
  548. offset = offsetof(struct ar_buffer, data);
  549. ab_bus = le32_to_cpu(ab->descriptor.data_address) - offset;
  550. reg_write(ctx->ohci, COMMAND_PTR(ctx->regs), ab_bus | 1);
  551. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN);
  552. flush_writes(ctx->ohci);
  553. }
  554. static struct descriptor *
  555. find_branch_descriptor(struct descriptor *d, int z)
  556. {
  557. int b, key;
  558. b = (le16_to_cpu(d->control) & DESCRIPTOR_BRANCH_ALWAYS) >> 2;
  559. key = (le16_to_cpu(d->control) & DESCRIPTOR_KEY_IMMEDIATE) >> 8;
  560. /* figure out which descriptor the branch address goes in */
  561. if (z == 2 && (b == 3 || key == 2))
  562. return d;
  563. else
  564. return d + z - 1;
  565. }
  566. static void context_tasklet(unsigned long data)
  567. {
  568. struct context *ctx = (struct context *) data;
  569. struct descriptor *d, *last;
  570. u32 address;
  571. int z;
  572. struct descriptor_buffer *desc;
  573. desc = list_entry(ctx->buffer_list.next,
  574. struct descriptor_buffer, list);
  575. last = ctx->last;
  576. while (last->branch_address != 0) {
  577. struct descriptor_buffer *old_desc = desc;
  578. address = le32_to_cpu(last->branch_address);
  579. z = address & 0xf;
  580. address &= ~0xf;
  581. /* If the branch address points to a buffer outside of the
  582. * current buffer, advance to the next buffer. */
  583. if (address < desc->buffer_bus ||
  584. address >= desc->buffer_bus + desc->used)
  585. desc = list_entry(desc->list.next,
  586. struct descriptor_buffer, list);
  587. d = desc->buffer + (address - desc->buffer_bus) / sizeof(*d);
  588. last = find_branch_descriptor(d, z);
  589. if (!ctx->callback(ctx, d, last))
  590. break;
  591. if (old_desc != desc) {
  592. /* If we've advanced to the next buffer, move the
  593. * previous buffer to the free list. */
  594. unsigned long flags;
  595. old_desc->used = 0;
  596. spin_lock_irqsave(&ctx->ohci->lock, flags);
  597. list_move_tail(&old_desc->list, &ctx->buffer_list);
  598. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  599. }
  600. ctx->last = last;
  601. }
  602. }
  603. /*
  604. * Allocate a new buffer and add it to the list of free buffers for this
  605. * context. Must be called with ohci->lock held.
  606. */
  607. static int
  608. context_add_buffer(struct context *ctx)
  609. {
  610. struct descriptor_buffer *desc;
  611. dma_addr_t uninitialized_var(bus_addr);
  612. int offset;
  613. /*
  614. * 16MB of descriptors should be far more than enough for any DMA
  615. * program. This will catch run-away userspace or DoS attacks.
  616. */
  617. if (ctx->total_allocation >= 16*1024*1024)
  618. return -ENOMEM;
  619. desc = dma_alloc_coherent(ctx->ohci->card.device, PAGE_SIZE,
  620. &bus_addr, GFP_ATOMIC);
  621. if (!desc)
  622. return -ENOMEM;
  623. offset = (void *)&desc->buffer - (void *)desc;
  624. desc->buffer_size = PAGE_SIZE - offset;
  625. desc->buffer_bus = bus_addr + offset;
  626. desc->used = 0;
  627. list_add_tail(&desc->list, &ctx->buffer_list);
  628. ctx->total_allocation += PAGE_SIZE;
  629. return 0;
  630. }
  631. static int
  632. context_init(struct context *ctx, struct fw_ohci *ohci,
  633. u32 regs, descriptor_callback_t callback)
  634. {
  635. ctx->ohci = ohci;
  636. ctx->regs = regs;
  637. ctx->total_allocation = 0;
  638. INIT_LIST_HEAD(&ctx->buffer_list);
  639. if (context_add_buffer(ctx) < 0)
  640. return -ENOMEM;
  641. ctx->buffer_tail = list_entry(ctx->buffer_list.next,
  642. struct descriptor_buffer, list);
  643. tasklet_init(&ctx->tasklet, context_tasklet, (unsigned long)ctx);
  644. ctx->callback = callback;
  645. /*
  646. * We put a dummy descriptor in the buffer that has a NULL
  647. * branch address and looks like it's been sent. That way we
  648. * have a descriptor to append DMA programs to.
  649. */
  650. memset(ctx->buffer_tail->buffer, 0, sizeof(*ctx->buffer_tail->buffer));
  651. ctx->buffer_tail->buffer->control = cpu_to_le16(DESCRIPTOR_OUTPUT_LAST);
  652. ctx->buffer_tail->buffer->transfer_status = cpu_to_le16(0x8011);
  653. ctx->buffer_tail->used += sizeof(*ctx->buffer_tail->buffer);
  654. ctx->last = ctx->buffer_tail->buffer;
  655. ctx->prev = ctx->buffer_tail->buffer;
  656. return 0;
  657. }
  658. static void
  659. context_release(struct context *ctx)
  660. {
  661. struct fw_card *card = &ctx->ohci->card;
  662. struct descriptor_buffer *desc, *tmp;
  663. list_for_each_entry_safe(desc, tmp, &ctx->buffer_list, list)
  664. dma_free_coherent(card->device, PAGE_SIZE, desc,
  665. desc->buffer_bus -
  666. ((void *)&desc->buffer - (void *)desc));
  667. }
  668. /* Must be called with ohci->lock held */
  669. static struct descriptor *
  670. context_get_descriptors(struct context *ctx, int z, dma_addr_t *d_bus)
  671. {
  672. struct descriptor *d = NULL;
  673. struct descriptor_buffer *desc = ctx->buffer_tail;
  674. if (z * sizeof(*d) > desc->buffer_size)
  675. return NULL;
  676. if (z * sizeof(*d) > desc->buffer_size - desc->used) {
  677. /* No room for the descriptor in this buffer, so advance to the
  678. * next one. */
  679. if (desc->list.next == &ctx->buffer_list) {
  680. /* If there is no free buffer next in the list,
  681. * allocate one. */
  682. if (context_add_buffer(ctx) < 0)
  683. return NULL;
  684. }
  685. desc = list_entry(desc->list.next,
  686. struct descriptor_buffer, list);
  687. ctx->buffer_tail = desc;
  688. }
  689. d = desc->buffer + desc->used / sizeof(*d);
  690. memset(d, 0, z * sizeof(*d));
  691. *d_bus = desc->buffer_bus + desc->used;
  692. return d;
  693. }
  694. static void context_run(struct context *ctx, u32 extra)
  695. {
  696. struct fw_ohci *ohci = ctx->ohci;
  697. reg_write(ohci, COMMAND_PTR(ctx->regs),
  698. le32_to_cpu(ctx->last->branch_address));
  699. reg_write(ohci, CONTROL_CLEAR(ctx->regs), ~0);
  700. reg_write(ohci, CONTROL_SET(ctx->regs), CONTEXT_RUN | extra);
  701. flush_writes(ohci);
  702. }
  703. static void context_append(struct context *ctx,
  704. struct descriptor *d, int z, int extra)
  705. {
  706. dma_addr_t d_bus;
  707. struct descriptor_buffer *desc = ctx->buffer_tail;
  708. d_bus = desc->buffer_bus + (d - desc->buffer) * sizeof(*d);
  709. desc->used += (z + extra) * sizeof(*d);
  710. ctx->prev->branch_address = cpu_to_le32(d_bus | z);
  711. ctx->prev = find_branch_descriptor(d, z);
  712. reg_write(ctx->ohci, CONTROL_SET(ctx->regs), CONTEXT_WAKE);
  713. flush_writes(ctx->ohci);
  714. }
  715. static void context_stop(struct context *ctx)
  716. {
  717. u32 reg;
  718. int i;
  719. reg_write(ctx->ohci, CONTROL_CLEAR(ctx->regs), CONTEXT_RUN);
  720. flush_writes(ctx->ohci);
  721. for (i = 0; i < 10; i++) {
  722. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  723. if ((reg & CONTEXT_ACTIVE) == 0)
  724. break;
  725. fw_notify("context_stop: still active (0x%08x)\n", reg);
  726. mdelay(1);
  727. }
  728. }
  729. struct driver_data {
  730. struct fw_packet *packet;
  731. };
  732. /*
  733. * This function apppends a packet to the DMA queue for transmission.
  734. * Must always be called with the ochi->lock held to ensure proper
  735. * generation handling and locking around packet queue manipulation.
  736. */
  737. static int
  738. at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
  739. {
  740. struct fw_ohci *ohci = ctx->ohci;
  741. dma_addr_t d_bus, uninitialized_var(payload_bus);
  742. struct driver_data *driver_data;
  743. struct descriptor *d, *last;
  744. __le32 *header;
  745. int z, tcode;
  746. u32 reg;
  747. d = context_get_descriptors(ctx, 4, &d_bus);
  748. if (d == NULL) {
  749. packet->ack = RCODE_SEND_ERROR;
  750. return -1;
  751. }
  752. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  753. d[0].res_count = cpu_to_le16(packet->timestamp);
  754. /*
  755. * The DMA format for asyncronous link packets is different
  756. * from the IEEE1394 layout, so shift the fields around
  757. * accordingly. If header_length is 8, it's a PHY packet, to
  758. * which we need to prepend an extra quadlet.
  759. */
  760. header = (__le32 *) &d[1];
  761. if (packet->header_length > 8) {
  762. header[0] = cpu_to_le32((packet->header[0] & 0xffff) |
  763. (packet->speed << 16));
  764. header[1] = cpu_to_le32((packet->header[1] & 0xffff) |
  765. (packet->header[0] & 0xffff0000));
  766. header[2] = cpu_to_le32(packet->header[2]);
  767. tcode = (packet->header[0] >> 4) & 0x0f;
  768. if (TCODE_IS_BLOCK_PACKET(tcode))
  769. header[3] = cpu_to_le32(packet->header[3]);
  770. else
  771. header[3] = (__force __le32) packet->header[3];
  772. d[0].req_count = cpu_to_le16(packet->header_length);
  773. } else {
  774. header[0] = cpu_to_le32((OHCI1394_phy_tcode << 4) |
  775. (packet->speed << 16));
  776. header[1] = cpu_to_le32(packet->header[0]);
  777. header[2] = cpu_to_le32(packet->header[1]);
  778. d[0].req_count = cpu_to_le16(12);
  779. }
  780. driver_data = (struct driver_data *) &d[3];
  781. driver_data->packet = packet;
  782. packet->driver_data = driver_data;
  783. if (packet->payload_length > 0) {
  784. payload_bus =
  785. dma_map_single(ohci->card.device, packet->payload,
  786. packet->payload_length, DMA_TO_DEVICE);
  787. if (dma_mapping_error(payload_bus)) {
  788. packet->ack = RCODE_SEND_ERROR;
  789. return -1;
  790. }
  791. d[2].req_count = cpu_to_le16(packet->payload_length);
  792. d[2].data_address = cpu_to_le32(payload_bus);
  793. last = &d[2];
  794. z = 3;
  795. } else {
  796. last = &d[0];
  797. z = 2;
  798. }
  799. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  800. DESCRIPTOR_IRQ_ALWAYS |
  801. DESCRIPTOR_BRANCH_ALWAYS);
  802. /* FIXME: Document how the locking works. */
  803. if (ohci->generation != packet->generation) {
  804. if (packet->payload_length > 0)
  805. dma_unmap_single(ohci->card.device, payload_bus,
  806. packet->payload_length, DMA_TO_DEVICE);
  807. packet->ack = RCODE_GENERATION;
  808. return -1;
  809. }
  810. context_append(ctx, d, z, 4 - z);
  811. /* If the context isn't already running, start it up. */
  812. reg = reg_read(ctx->ohci, CONTROL_SET(ctx->regs));
  813. if ((reg & CONTEXT_RUN) == 0)
  814. context_run(ctx, 0);
  815. return 0;
  816. }
  817. static int handle_at_packet(struct context *context,
  818. struct descriptor *d,
  819. struct descriptor *last)
  820. {
  821. struct driver_data *driver_data;
  822. struct fw_packet *packet;
  823. struct fw_ohci *ohci = context->ohci;
  824. dma_addr_t payload_bus;
  825. int evt;
  826. if (last->transfer_status == 0)
  827. /* This descriptor isn't done yet, stop iteration. */
  828. return 0;
  829. driver_data = (struct driver_data *) &d[3];
  830. packet = driver_data->packet;
  831. if (packet == NULL)
  832. /* This packet was cancelled, just continue. */
  833. return 1;
  834. payload_bus = le32_to_cpu(last->data_address);
  835. if (payload_bus != 0)
  836. dma_unmap_single(ohci->card.device, payload_bus,
  837. packet->payload_length, DMA_TO_DEVICE);
  838. evt = le16_to_cpu(last->transfer_status) & 0x1f;
  839. packet->timestamp = le16_to_cpu(last->res_count);
  840. log_ar_at_event('T', packet->speed, packet->header, evt);
  841. switch (evt) {
  842. case OHCI1394_evt_timeout:
  843. /* Async response transmit timed out. */
  844. packet->ack = RCODE_CANCELLED;
  845. break;
  846. case OHCI1394_evt_flushed:
  847. /*
  848. * The packet was flushed should give same error as
  849. * when we try to use a stale generation count.
  850. */
  851. packet->ack = RCODE_GENERATION;
  852. break;
  853. case OHCI1394_evt_missing_ack:
  854. /*
  855. * Using a valid (current) generation count, but the
  856. * node is not on the bus or not sending acks.
  857. */
  858. packet->ack = RCODE_NO_ACK;
  859. break;
  860. case ACK_COMPLETE + 0x10:
  861. case ACK_PENDING + 0x10:
  862. case ACK_BUSY_X + 0x10:
  863. case ACK_BUSY_A + 0x10:
  864. case ACK_BUSY_B + 0x10:
  865. case ACK_DATA_ERROR + 0x10:
  866. case ACK_TYPE_ERROR + 0x10:
  867. packet->ack = evt - 0x10;
  868. break;
  869. default:
  870. packet->ack = RCODE_SEND_ERROR;
  871. break;
  872. }
  873. packet->callback(packet, &ohci->card, packet->ack);
  874. return 1;
  875. }
  876. #define HEADER_GET_DESTINATION(q) (((q) >> 16) & 0xffff)
  877. #define HEADER_GET_TCODE(q) (((q) >> 4) & 0x0f)
  878. #define HEADER_GET_OFFSET_HIGH(q) (((q) >> 0) & 0xffff)
  879. #define HEADER_GET_DATA_LENGTH(q) (((q) >> 16) & 0xffff)
  880. #define HEADER_GET_EXTENDED_TCODE(q) (((q) >> 0) & 0xffff)
  881. static void
  882. handle_local_rom(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  883. {
  884. struct fw_packet response;
  885. int tcode, length, i;
  886. tcode = HEADER_GET_TCODE(packet->header[0]);
  887. if (TCODE_IS_BLOCK_PACKET(tcode))
  888. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  889. else
  890. length = 4;
  891. i = csr - CSR_CONFIG_ROM;
  892. if (i + length > CONFIG_ROM_SIZE) {
  893. fw_fill_response(&response, packet->header,
  894. RCODE_ADDRESS_ERROR, NULL, 0);
  895. } else if (!TCODE_IS_READ_REQUEST(tcode)) {
  896. fw_fill_response(&response, packet->header,
  897. RCODE_TYPE_ERROR, NULL, 0);
  898. } else {
  899. fw_fill_response(&response, packet->header, RCODE_COMPLETE,
  900. (void *) ohci->config_rom + i, length);
  901. }
  902. fw_core_handle_response(&ohci->card, &response);
  903. }
  904. static void
  905. handle_local_lock(struct fw_ohci *ohci, struct fw_packet *packet, u32 csr)
  906. {
  907. struct fw_packet response;
  908. int tcode, length, ext_tcode, sel;
  909. __be32 *payload, lock_old;
  910. u32 lock_arg, lock_data;
  911. tcode = HEADER_GET_TCODE(packet->header[0]);
  912. length = HEADER_GET_DATA_LENGTH(packet->header[3]);
  913. payload = packet->payload;
  914. ext_tcode = HEADER_GET_EXTENDED_TCODE(packet->header[3]);
  915. if (tcode == TCODE_LOCK_REQUEST &&
  916. ext_tcode == EXTCODE_COMPARE_SWAP && length == 8) {
  917. lock_arg = be32_to_cpu(payload[0]);
  918. lock_data = be32_to_cpu(payload[1]);
  919. } else if (tcode == TCODE_READ_QUADLET_REQUEST) {
  920. lock_arg = 0;
  921. lock_data = 0;
  922. } else {
  923. fw_fill_response(&response, packet->header,
  924. RCODE_TYPE_ERROR, NULL, 0);
  925. goto out;
  926. }
  927. sel = (csr - CSR_BUS_MANAGER_ID) / 4;
  928. reg_write(ohci, OHCI1394_CSRData, lock_data);
  929. reg_write(ohci, OHCI1394_CSRCompareData, lock_arg);
  930. reg_write(ohci, OHCI1394_CSRControl, sel);
  931. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
  932. lock_old = cpu_to_be32(reg_read(ohci, OHCI1394_CSRData));
  933. else
  934. fw_notify("swap not done yet\n");
  935. fw_fill_response(&response, packet->header,
  936. RCODE_COMPLETE, &lock_old, sizeof(lock_old));
  937. out:
  938. fw_core_handle_response(&ohci->card, &response);
  939. }
  940. static void
  941. handle_local_request(struct context *ctx, struct fw_packet *packet)
  942. {
  943. u64 offset;
  944. u32 csr;
  945. if (ctx == &ctx->ohci->at_request_ctx) {
  946. packet->ack = ACK_PENDING;
  947. packet->callback(packet, &ctx->ohci->card, packet->ack);
  948. }
  949. offset =
  950. ((unsigned long long)
  951. HEADER_GET_OFFSET_HIGH(packet->header[1]) << 32) |
  952. packet->header[2];
  953. csr = offset - CSR_REGISTER_BASE;
  954. /* Handle config rom reads. */
  955. if (csr >= CSR_CONFIG_ROM && csr < CSR_CONFIG_ROM_END)
  956. handle_local_rom(ctx->ohci, packet, csr);
  957. else switch (csr) {
  958. case CSR_BUS_MANAGER_ID:
  959. case CSR_BANDWIDTH_AVAILABLE:
  960. case CSR_CHANNELS_AVAILABLE_HI:
  961. case CSR_CHANNELS_AVAILABLE_LO:
  962. handle_local_lock(ctx->ohci, packet, csr);
  963. break;
  964. default:
  965. if (ctx == &ctx->ohci->at_request_ctx)
  966. fw_core_handle_request(&ctx->ohci->card, packet);
  967. else
  968. fw_core_handle_response(&ctx->ohci->card, packet);
  969. break;
  970. }
  971. if (ctx == &ctx->ohci->at_response_ctx) {
  972. packet->ack = ACK_COMPLETE;
  973. packet->callback(packet, &ctx->ohci->card, packet->ack);
  974. }
  975. }
  976. static void
  977. at_context_transmit(struct context *ctx, struct fw_packet *packet)
  978. {
  979. unsigned long flags;
  980. int retval;
  981. spin_lock_irqsave(&ctx->ohci->lock, flags);
  982. if (HEADER_GET_DESTINATION(packet->header[0]) == ctx->ohci->node_id &&
  983. ctx->ohci->generation == packet->generation) {
  984. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  985. handle_local_request(ctx, packet);
  986. return;
  987. }
  988. retval = at_context_queue_packet(ctx, packet);
  989. spin_unlock_irqrestore(&ctx->ohci->lock, flags);
  990. if (retval < 0)
  991. packet->callback(packet, &ctx->ohci->card, packet->ack);
  992. }
  993. static void bus_reset_tasklet(unsigned long data)
  994. {
  995. struct fw_ohci *ohci = (struct fw_ohci *)data;
  996. int self_id_count, i, j, reg;
  997. int generation, new_generation;
  998. unsigned long flags;
  999. void *free_rom = NULL;
  1000. dma_addr_t free_rom_bus = 0;
  1001. reg = reg_read(ohci, OHCI1394_NodeID);
  1002. if (!(reg & OHCI1394_NodeID_idValid)) {
  1003. fw_notify("node ID not valid, new bus reset in progress\n");
  1004. return;
  1005. }
  1006. if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
  1007. fw_notify("malconfigured bus\n");
  1008. return;
  1009. }
  1010. ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
  1011. OHCI1394_NodeID_nodeNumber);
  1012. reg = reg_read(ohci, OHCI1394_SelfIDCount);
  1013. if (reg & OHCI1394_SelfIDCount_selfIDError) {
  1014. fw_notify("inconsistent self IDs\n");
  1015. return;
  1016. }
  1017. /*
  1018. * The count in the SelfIDCount register is the number of
  1019. * bytes in the self ID receive buffer. Since we also receive
  1020. * the inverted quadlets and a header quadlet, we shift one
  1021. * bit extra to get the actual number of self IDs.
  1022. */
  1023. self_id_count = (reg >> 3) & 0x3ff;
  1024. if (self_id_count == 0) {
  1025. fw_notify("inconsistent self IDs\n");
  1026. return;
  1027. }
  1028. generation = (cond_le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
  1029. rmb();
  1030. for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
  1031. if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1]) {
  1032. fw_notify("inconsistent self IDs\n");
  1033. return;
  1034. }
  1035. ohci->self_id_buffer[j] =
  1036. cond_le32_to_cpu(ohci->self_id_cpu[i]);
  1037. }
  1038. rmb();
  1039. /*
  1040. * Check the consistency of the self IDs we just read. The
  1041. * problem we face is that a new bus reset can start while we
  1042. * read out the self IDs from the DMA buffer. If this happens,
  1043. * the DMA buffer will be overwritten with new self IDs and we
  1044. * will read out inconsistent data. The OHCI specification
  1045. * (section 11.2) recommends a technique similar to
  1046. * linux/seqlock.h, where we remember the generation of the
  1047. * self IDs in the buffer before reading them out and compare
  1048. * it to the current generation after reading them out. If
  1049. * the two generations match we know we have a consistent set
  1050. * of self IDs.
  1051. */
  1052. new_generation = (reg_read(ohci, OHCI1394_SelfIDCount) >> 16) & 0xff;
  1053. if (new_generation != generation) {
  1054. fw_notify("recursive bus reset detected, "
  1055. "discarding self ids\n");
  1056. return;
  1057. }
  1058. /* FIXME: Document how the locking works. */
  1059. spin_lock_irqsave(&ohci->lock, flags);
  1060. ohci->generation = generation;
  1061. context_stop(&ohci->at_request_ctx);
  1062. context_stop(&ohci->at_response_ctx);
  1063. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  1064. /*
  1065. * This next bit is unrelated to the AT context stuff but we
  1066. * have to do it under the spinlock also. If a new config rom
  1067. * was set up before this reset, the old one is now no longer
  1068. * in use and we can free it. Update the config rom pointers
  1069. * to point to the current config rom and clear the
  1070. * next_config_rom pointer so a new udpate can take place.
  1071. */
  1072. if (ohci->next_config_rom != NULL) {
  1073. if (ohci->next_config_rom != ohci->config_rom) {
  1074. free_rom = ohci->config_rom;
  1075. free_rom_bus = ohci->config_rom_bus;
  1076. }
  1077. ohci->config_rom = ohci->next_config_rom;
  1078. ohci->config_rom_bus = ohci->next_config_rom_bus;
  1079. ohci->next_config_rom = NULL;
  1080. /*
  1081. * Restore config_rom image and manually update
  1082. * config_rom registers. Writing the header quadlet
  1083. * will indicate that the config rom is ready, so we
  1084. * do that last.
  1085. */
  1086. reg_write(ohci, OHCI1394_BusOptions,
  1087. be32_to_cpu(ohci->config_rom[2]));
  1088. ohci->config_rom[0] = cpu_to_be32(ohci->next_header);
  1089. reg_write(ohci, OHCI1394_ConfigROMhdr, ohci->next_header);
  1090. }
  1091. #ifdef CONFIG_FIREWIRE_OHCI_REMOTE_DMA
  1092. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, ~0);
  1093. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, ~0);
  1094. #endif
  1095. spin_unlock_irqrestore(&ohci->lock, flags);
  1096. if (free_rom)
  1097. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1098. free_rom, free_rom_bus);
  1099. log_selfids(generation, self_id_count, ohci->self_id_buffer);
  1100. fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
  1101. self_id_count, ohci->self_id_buffer);
  1102. }
  1103. static irqreturn_t irq_handler(int irq, void *data)
  1104. {
  1105. struct fw_ohci *ohci = data;
  1106. u32 event, iso_event, cycle_time;
  1107. int i;
  1108. event = reg_read(ohci, OHCI1394_IntEventClear);
  1109. if (!event || !~event)
  1110. return IRQ_NONE;
  1111. reg_write(ohci, OHCI1394_IntEventClear, event);
  1112. log_irqs(event);
  1113. if (event & OHCI1394_selfIDComplete)
  1114. tasklet_schedule(&ohci->bus_reset_tasklet);
  1115. if (event & OHCI1394_RQPkt)
  1116. tasklet_schedule(&ohci->ar_request_ctx.tasklet);
  1117. if (event & OHCI1394_RSPkt)
  1118. tasklet_schedule(&ohci->ar_response_ctx.tasklet);
  1119. if (event & OHCI1394_reqTxComplete)
  1120. tasklet_schedule(&ohci->at_request_ctx.tasklet);
  1121. if (event & OHCI1394_respTxComplete)
  1122. tasklet_schedule(&ohci->at_response_ctx.tasklet);
  1123. iso_event = reg_read(ohci, OHCI1394_IsoRecvIntEventClear);
  1124. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, iso_event);
  1125. while (iso_event) {
  1126. i = ffs(iso_event) - 1;
  1127. tasklet_schedule(&ohci->ir_context_list[i].context.tasklet);
  1128. iso_event &= ~(1 << i);
  1129. }
  1130. iso_event = reg_read(ohci, OHCI1394_IsoXmitIntEventClear);
  1131. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, iso_event);
  1132. while (iso_event) {
  1133. i = ffs(iso_event) - 1;
  1134. tasklet_schedule(&ohci->it_context_list[i].context.tasklet);
  1135. iso_event &= ~(1 << i);
  1136. }
  1137. if (unlikely(event & OHCI1394_postedWriteErr))
  1138. fw_error("PCI posted write error\n");
  1139. if (unlikely(event & OHCI1394_cycleTooLong)) {
  1140. if (printk_ratelimit())
  1141. fw_notify("isochronous cycle too long\n");
  1142. reg_write(ohci, OHCI1394_LinkControlSet,
  1143. OHCI1394_LinkControl_cycleMaster);
  1144. }
  1145. if (event & OHCI1394_cycle64Seconds) {
  1146. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1147. if ((cycle_time & 0x80000000) == 0)
  1148. ohci->bus_seconds++;
  1149. }
  1150. return IRQ_HANDLED;
  1151. }
  1152. static int software_reset(struct fw_ohci *ohci)
  1153. {
  1154. int i;
  1155. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  1156. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  1157. if ((reg_read(ohci, OHCI1394_HCControlSet) &
  1158. OHCI1394_HCControl_softReset) == 0)
  1159. return 0;
  1160. msleep(1);
  1161. }
  1162. return -EBUSY;
  1163. }
  1164. static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
  1165. {
  1166. struct fw_ohci *ohci = fw_ohci(card);
  1167. struct pci_dev *dev = to_pci_dev(card->device);
  1168. if (software_reset(ohci)) {
  1169. fw_error("Failed to reset ohci card.\n");
  1170. return -EBUSY;
  1171. }
  1172. /*
  1173. * Now enable LPS, which we need in order to start accessing
  1174. * most of the registers. In fact, on some cards (ALI M5251),
  1175. * accessing registers in the SClk domain without LPS enabled
  1176. * will lock up the machine. Wait 50msec to make sure we have
  1177. * full link enabled.
  1178. */
  1179. reg_write(ohci, OHCI1394_HCControlSet,
  1180. OHCI1394_HCControl_LPS |
  1181. OHCI1394_HCControl_postedWriteEnable);
  1182. flush_writes(ohci);
  1183. msleep(50);
  1184. reg_write(ohci, OHCI1394_HCControlClear,
  1185. OHCI1394_HCControl_noByteSwapData);
  1186. reg_write(ohci, OHCI1394_LinkControlSet,
  1187. OHCI1394_LinkControl_rcvSelfID |
  1188. OHCI1394_LinkControl_cycleTimerEnable |
  1189. OHCI1394_LinkControl_cycleMaster);
  1190. reg_write(ohci, OHCI1394_ATRetries,
  1191. OHCI1394_MAX_AT_REQ_RETRIES |
  1192. (OHCI1394_MAX_AT_RESP_RETRIES << 4) |
  1193. (OHCI1394_MAX_PHYS_RESP_RETRIES << 8));
  1194. ar_context_run(&ohci->ar_request_ctx);
  1195. ar_context_run(&ohci->ar_response_ctx);
  1196. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
  1197. reg_write(ohci, OHCI1394_PhyUpperBound, 0x00010000);
  1198. reg_write(ohci, OHCI1394_IntEventClear, ~0);
  1199. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  1200. reg_write(ohci, OHCI1394_IntMaskSet,
  1201. OHCI1394_selfIDComplete |
  1202. OHCI1394_RQPkt | OHCI1394_RSPkt |
  1203. OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
  1204. OHCI1394_isochRx | OHCI1394_isochTx |
  1205. OHCI1394_postedWriteErr | OHCI1394_cycleTooLong |
  1206. OHCI1394_cycle64Seconds | OHCI1394_masterIntEnable);
  1207. /* Activate link_on bit and contender bit in our self ID packets.*/
  1208. if (ohci_update_phy_reg(card, 4, 0,
  1209. PHY_LINK_ACTIVE | PHY_CONTENDER) < 0)
  1210. return -EIO;
  1211. /*
  1212. * When the link is not yet enabled, the atomic config rom
  1213. * update mechanism described below in ohci_set_config_rom()
  1214. * is not active. We have to update ConfigRomHeader and
  1215. * BusOptions manually, and the write to ConfigROMmap takes
  1216. * effect immediately. We tie this to the enabling of the
  1217. * link, so we have a valid config rom before enabling - the
  1218. * OHCI requires that ConfigROMhdr and BusOptions have valid
  1219. * values before enabling.
  1220. *
  1221. * However, when the ConfigROMmap is written, some controllers
  1222. * always read back quadlets 0 and 2 from the config rom to
  1223. * the ConfigRomHeader and BusOptions registers on bus reset.
  1224. * They shouldn't do that in this initial case where the link
  1225. * isn't enabled. This means we have to use the same
  1226. * workaround here, setting the bus header to 0 and then write
  1227. * the right values in the bus reset tasklet.
  1228. */
  1229. if (config_rom) {
  1230. ohci->next_config_rom =
  1231. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1232. &ohci->next_config_rom_bus,
  1233. GFP_KERNEL);
  1234. if (ohci->next_config_rom == NULL)
  1235. return -ENOMEM;
  1236. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  1237. fw_memcpy_to_be32(ohci->next_config_rom, config_rom, length * 4);
  1238. } else {
  1239. /*
  1240. * In the suspend case, config_rom is NULL, which
  1241. * means that we just reuse the old config rom.
  1242. */
  1243. ohci->next_config_rom = ohci->config_rom;
  1244. ohci->next_config_rom_bus = ohci->config_rom_bus;
  1245. }
  1246. ohci->next_header = be32_to_cpu(ohci->next_config_rom[0]);
  1247. ohci->next_config_rom[0] = 0;
  1248. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  1249. reg_write(ohci, OHCI1394_BusOptions,
  1250. be32_to_cpu(ohci->next_config_rom[2]));
  1251. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->next_config_rom_bus);
  1252. reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
  1253. if (request_irq(dev->irq, irq_handler,
  1254. IRQF_SHARED, ohci_driver_name, ohci)) {
  1255. fw_error("Failed to allocate shared interrupt %d.\n",
  1256. dev->irq);
  1257. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1258. ohci->config_rom, ohci->config_rom_bus);
  1259. return -EIO;
  1260. }
  1261. reg_write(ohci, OHCI1394_HCControlSet,
  1262. OHCI1394_HCControl_linkEnable |
  1263. OHCI1394_HCControl_BIBimageValid);
  1264. flush_writes(ohci);
  1265. /*
  1266. * We are ready to go, initiate bus reset to finish the
  1267. * initialization.
  1268. */
  1269. fw_core_initiate_bus_reset(&ohci->card, 1);
  1270. return 0;
  1271. }
  1272. static int
  1273. ohci_set_config_rom(struct fw_card *card, u32 *config_rom, size_t length)
  1274. {
  1275. struct fw_ohci *ohci;
  1276. unsigned long flags;
  1277. int retval = -EBUSY;
  1278. __be32 *next_config_rom;
  1279. dma_addr_t uninitialized_var(next_config_rom_bus);
  1280. ohci = fw_ohci(card);
  1281. /*
  1282. * When the OHCI controller is enabled, the config rom update
  1283. * mechanism is a bit tricky, but easy enough to use. See
  1284. * section 5.5.6 in the OHCI specification.
  1285. *
  1286. * The OHCI controller caches the new config rom address in a
  1287. * shadow register (ConfigROMmapNext) and needs a bus reset
  1288. * for the changes to take place. When the bus reset is
  1289. * detected, the controller loads the new values for the
  1290. * ConfigRomHeader and BusOptions registers from the specified
  1291. * config rom and loads ConfigROMmap from the ConfigROMmapNext
  1292. * shadow register. All automatically and atomically.
  1293. *
  1294. * Now, there's a twist to this story. The automatic load of
  1295. * ConfigRomHeader and BusOptions doesn't honor the
  1296. * noByteSwapData bit, so with a be32 config rom, the
  1297. * controller will load be32 values in to these registers
  1298. * during the atomic update, even on litte endian
  1299. * architectures. The workaround we use is to put a 0 in the
  1300. * header quadlet; 0 is endian agnostic and means that the
  1301. * config rom isn't ready yet. In the bus reset tasklet we
  1302. * then set up the real values for the two registers.
  1303. *
  1304. * We use ohci->lock to avoid racing with the code that sets
  1305. * ohci->next_config_rom to NULL (see bus_reset_tasklet).
  1306. */
  1307. next_config_rom =
  1308. dma_alloc_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1309. &next_config_rom_bus, GFP_KERNEL);
  1310. if (next_config_rom == NULL)
  1311. return -ENOMEM;
  1312. spin_lock_irqsave(&ohci->lock, flags);
  1313. if (ohci->next_config_rom == NULL) {
  1314. ohci->next_config_rom = next_config_rom;
  1315. ohci->next_config_rom_bus = next_config_rom_bus;
  1316. memset(ohci->next_config_rom, 0, CONFIG_ROM_SIZE);
  1317. fw_memcpy_to_be32(ohci->next_config_rom, config_rom,
  1318. length * 4);
  1319. ohci->next_header = config_rom[0];
  1320. ohci->next_config_rom[0] = 0;
  1321. reg_write(ohci, OHCI1394_ConfigROMmap,
  1322. ohci->next_config_rom_bus);
  1323. retval = 0;
  1324. }
  1325. spin_unlock_irqrestore(&ohci->lock, flags);
  1326. /*
  1327. * Now initiate a bus reset to have the changes take
  1328. * effect. We clean up the old config rom memory and DMA
  1329. * mappings in the bus reset tasklet, since the OHCI
  1330. * controller could need to access it before the bus reset
  1331. * takes effect.
  1332. */
  1333. if (retval == 0)
  1334. fw_core_initiate_bus_reset(&ohci->card, 1);
  1335. else
  1336. dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
  1337. next_config_rom, next_config_rom_bus);
  1338. return retval;
  1339. }
  1340. static void ohci_send_request(struct fw_card *card, struct fw_packet *packet)
  1341. {
  1342. struct fw_ohci *ohci = fw_ohci(card);
  1343. at_context_transmit(&ohci->at_request_ctx, packet);
  1344. }
  1345. static void ohci_send_response(struct fw_card *card, struct fw_packet *packet)
  1346. {
  1347. struct fw_ohci *ohci = fw_ohci(card);
  1348. at_context_transmit(&ohci->at_response_ctx, packet);
  1349. }
  1350. static int ohci_cancel_packet(struct fw_card *card, struct fw_packet *packet)
  1351. {
  1352. struct fw_ohci *ohci = fw_ohci(card);
  1353. struct context *ctx = &ohci->at_request_ctx;
  1354. struct driver_data *driver_data = packet->driver_data;
  1355. int retval = -ENOENT;
  1356. tasklet_disable(&ctx->tasklet);
  1357. if (packet->ack != 0)
  1358. goto out;
  1359. log_ar_at_event('T', packet->speed, packet->header, 0x20);
  1360. driver_data->packet = NULL;
  1361. packet->ack = RCODE_CANCELLED;
  1362. packet->callback(packet, &ohci->card, packet->ack);
  1363. retval = 0;
  1364. out:
  1365. tasklet_enable(&ctx->tasklet);
  1366. return retval;
  1367. }
  1368. static int
  1369. ohci_enable_phys_dma(struct fw_card *card, int node_id, int generation)
  1370. {
  1371. #ifdef CONFIG_FIREWIRE_OHCI_REMOTE_DMA
  1372. return 0;
  1373. #else
  1374. struct fw_ohci *ohci = fw_ohci(card);
  1375. unsigned long flags;
  1376. int n, retval = 0;
  1377. /*
  1378. * FIXME: Make sure this bitmask is cleared when we clear the busReset
  1379. * interrupt bit. Clear physReqResourceAllBuses on bus reset.
  1380. */
  1381. spin_lock_irqsave(&ohci->lock, flags);
  1382. if (ohci->generation != generation) {
  1383. retval = -ESTALE;
  1384. goto out;
  1385. }
  1386. /*
  1387. * Note, if the node ID contains a non-local bus ID, physical DMA is
  1388. * enabled for _all_ nodes on remote buses.
  1389. */
  1390. n = (node_id & 0xffc0) == LOCAL_BUS ? node_id & 0x3f : 63;
  1391. if (n < 32)
  1392. reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 1 << n);
  1393. else
  1394. reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 1 << (n - 32));
  1395. flush_writes(ohci);
  1396. out:
  1397. spin_unlock_irqrestore(&ohci->lock, flags);
  1398. return retval;
  1399. #endif /* CONFIG_FIREWIRE_OHCI_REMOTE_DMA */
  1400. }
  1401. static u64
  1402. ohci_get_bus_time(struct fw_card *card)
  1403. {
  1404. struct fw_ohci *ohci = fw_ohci(card);
  1405. u32 cycle_time;
  1406. u64 bus_time;
  1407. cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  1408. bus_time = ((u64) ohci->bus_seconds << 32) | cycle_time;
  1409. return bus_time;
  1410. }
  1411. static int handle_ir_dualbuffer_packet(struct context *context,
  1412. struct descriptor *d,
  1413. struct descriptor *last)
  1414. {
  1415. struct iso_context *ctx =
  1416. container_of(context, struct iso_context, context);
  1417. struct db_descriptor *db = (struct db_descriptor *) d;
  1418. __le32 *ir_header;
  1419. size_t header_length;
  1420. void *p, *end;
  1421. int i;
  1422. if (db->first_res_count != 0 && db->second_res_count != 0) {
  1423. if (ctx->excess_bytes <= le16_to_cpu(db->second_req_count)) {
  1424. /* This descriptor isn't done yet, stop iteration. */
  1425. return 0;
  1426. }
  1427. ctx->excess_bytes -= le16_to_cpu(db->second_req_count);
  1428. }
  1429. header_length = le16_to_cpu(db->first_req_count) -
  1430. le16_to_cpu(db->first_res_count);
  1431. i = ctx->header_length;
  1432. p = db + 1;
  1433. end = p + header_length;
  1434. while (p < end && i + ctx->base.header_size <= PAGE_SIZE) {
  1435. /*
  1436. * The iso header is byteswapped to little endian by
  1437. * the controller, but the remaining header quadlets
  1438. * are big endian. We want to present all the headers
  1439. * as big endian, so we have to swap the first
  1440. * quadlet.
  1441. */
  1442. *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
  1443. memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
  1444. i += ctx->base.header_size;
  1445. ctx->excess_bytes +=
  1446. (le32_to_cpu(*(__le32 *)(p + 4)) >> 16) & 0xffff;
  1447. p += ctx->base.header_size + 4;
  1448. }
  1449. ctx->header_length = i;
  1450. ctx->excess_bytes -= le16_to_cpu(db->second_req_count) -
  1451. le16_to_cpu(db->second_res_count);
  1452. if (le16_to_cpu(db->control) & DESCRIPTOR_IRQ_ALWAYS) {
  1453. ir_header = (__le32 *) (db + 1);
  1454. ctx->base.callback(&ctx->base,
  1455. le32_to_cpu(ir_header[0]) & 0xffff,
  1456. ctx->header_length, ctx->header,
  1457. ctx->base.callback_data);
  1458. ctx->header_length = 0;
  1459. }
  1460. return 1;
  1461. }
  1462. static int handle_ir_packet_per_buffer(struct context *context,
  1463. struct descriptor *d,
  1464. struct descriptor *last)
  1465. {
  1466. struct iso_context *ctx =
  1467. container_of(context, struct iso_context, context);
  1468. struct descriptor *pd;
  1469. __le32 *ir_header;
  1470. void *p;
  1471. int i;
  1472. for (pd = d; pd <= last; pd++) {
  1473. if (pd->transfer_status)
  1474. break;
  1475. }
  1476. if (pd > last)
  1477. /* Descriptor(s) not done yet, stop iteration */
  1478. return 0;
  1479. i = ctx->header_length;
  1480. p = last + 1;
  1481. if (ctx->base.header_size > 0 &&
  1482. i + ctx->base.header_size <= PAGE_SIZE) {
  1483. /*
  1484. * The iso header is byteswapped to little endian by
  1485. * the controller, but the remaining header quadlets
  1486. * are big endian. We want to present all the headers
  1487. * as big endian, so we have to swap the first quadlet.
  1488. */
  1489. *(u32 *) (ctx->header + i) = __swab32(*(u32 *) (p + 4));
  1490. memcpy(ctx->header + i + 4, p + 8, ctx->base.header_size - 4);
  1491. ctx->header_length += ctx->base.header_size;
  1492. }
  1493. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS) {
  1494. ir_header = (__le32 *) p;
  1495. ctx->base.callback(&ctx->base,
  1496. le32_to_cpu(ir_header[0]) & 0xffff,
  1497. ctx->header_length, ctx->header,
  1498. ctx->base.callback_data);
  1499. ctx->header_length = 0;
  1500. }
  1501. return 1;
  1502. }
  1503. static int handle_it_packet(struct context *context,
  1504. struct descriptor *d,
  1505. struct descriptor *last)
  1506. {
  1507. struct iso_context *ctx =
  1508. container_of(context, struct iso_context, context);
  1509. if (last->transfer_status == 0)
  1510. /* This descriptor isn't done yet, stop iteration. */
  1511. return 0;
  1512. if (le16_to_cpu(last->control) & DESCRIPTOR_IRQ_ALWAYS)
  1513. ctx->base.callback(&ctx->base, le16_to_cpu(last->res_count),
  1514. 0, NULL, ctx->base.callback_data);
  1515. return 1;
  1516. }
  1517. static struct fw_iso_context *
  1518. ohci_allocate_iso_context(struct fw_card *card, int type, size_t header_size)
  1519. {
  1520. struct fw_ohci *ohci = fw_ohci(card);
  1521. struct iso_context *ctx, *list;
  1522. descriptor_callback_t callback;
  1523. u32 *mask, regs;
  1524. unsigned long flags;
  1525. int index, retval = -ENOMEM;
  1526. if (type == FW_ISO_CONTEXT_TRANSMIT) {
  1527. mask = &ohci->it_context_mask;
  1528. list = ohci->it_context_list;
  1529. callback = handle_it_packet;
  1530. } else {
  1531. mask = &ohci->ir_context_mask;
  1532. list = ohci->ir_context_list;
  1533. if (ohci->version >= OHCI_VERSION_1_1)
  1534. callback = handle_ir_dualbuffer_packet;
  1535. else
  1536. callback = handle_ir_packet_per_buffer;
  1537. }
  1538. spin_lock_irqsave(&ohci->lock, flags);
  1539. index = ffs(*mask) - 1;
  1540. if (index >= 0)
  1541. *mask &= ~(1 << index);
  1542. spin_unlock_irqrestore(&ohci->lock, flags);
  1543. if (index < 0)
  1544. return ERR_PTR(-EBUSY);
  1545. if (type == FW_ISO_CONTEXT_TRANSMIT)
  1546. regs = OHCI1394_IsoXmitContextBase(index);
  1547. else
  1548. regs = OHCI1394_IsoRcvContextBase(index);
  1549. ctx = &list[index];
  1550. memset(ctx, 0, sizeof(*ctx));
  1551. ctx->header_length = 0;
  1552. ctx->header = (void *) __get_free_page(GFP_KERNEL);
  1553. if (ctx->header == NULL)
  1554. goto out;
  1555. retval = context_init(&ctx->context, ohci, regs, callback);
  1556. if (retval < 0)
  1557. goto out_with_header;
  1558. return &ctx->base;
  1559. out_with_header:
  1560. free_page((unsigned long)ctx->header);
  1561. out:
  1562. spin_lock_irqsave(&ohci->lock, flags);
  1563. *mask |= 1 << index;
  1564. spin_unlock_irqrestore(&ohci->lock, flags);
  1565. return ERR_PTR(retval);
  1566. }
  1567. static int ohci_start_iso(struct fw_iso_context *base,
  1568. s32 cycle, u32 sync, u32 tags)
  1569. {
  1570. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1571. struct fw_ohci *ohci = ctx->context.ohci;
  1572. u32 control, match;
  1573. int index;
  1574. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1575. index = ctx - ohci->it_context_list;
  1576. match = 0;
  1577. if (cycle >= 0)
  1578. match = IT_CONTEXT_CYCLE_MATCH_ENABLE |
  1579. (cycle & 0x7fff) << 16;
  1580. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 1 << index);
  1581. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << index);
  1582. context_run(&ctx->context, match);
  1583. } else {
  1584. index = ctx - ohci->ir_context_list;
  1585. control = IR_CONTEXT_ISOCH_HEADER;
  1586. if (ohci->version >= OHCI_VERSION_1_1)
  1587. control |= IR_CONTEXT_DUAL_BUFFER_MODE;
  1588. match = (tags << 28) | (sync << 8) | ctx->base.channel;
  1589. if (cycle >= 0) {
  1590. match |= (cycle & 0x07fff) << 12;
  1591. control |= IR_CONTEXT_CYCLE_MATCH_ENABLE;
  1592. }
  1593. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 1 << index);
  1594. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << index);
  1595. reg_write(ohci, CONTEXT_MATCH(ctx->context.regs), match);
  1596. context_run(&ctx->context, control);
  1597. }
  1598. return 0;
  1599. }
  1600. static int ohci_stop_iso(struct fw_iso_context *base)
  1601. {
  1602. struct fw_ohci *ohci = fw_ohci(base->card);
  1603. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1604. int index;
  1605. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1606. index = ctx - ohci->it_context_list;
  1607. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 1 << index);
  1608. } else {
  1609. index = ctx - ohci->ir_context_list;
  1610. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 1 << index);
  1611. }
  1612. flush_writes(ohci);
  1613. context_stop(&ctx->context);
  1614. return 0;
  1615. }
  1616. static void ohci_free_iso_context(struct fw_iso_context *base)
  1617. {
  1618. struct fw_ohci *ohci = fw_ohci(base->card);
  1619. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1620. unsigned long flags;
  1621. int index;
  1622. ohci_stop_iso(base);
  1623. context_release(&ctx->context);
  1624. free_page((unsigned long)ctx->header);
  1625. spin_lock_irqsave(&ohci->lock, flags);
  1626. if (ctx->base.type == FW_ISO_CONTEXT_TRANSMIT) {
  1627. index = ctx - ohci->it_context_list;
  1628. ohci->it_context_mask |= 1 << index;
  1629. } else {
  1630. index = ctx - ohci->ir_context_list;
  1631. ohci->ir_context_mask |= 1 << index;
  1632. }
  1633. spin_unlock_irqrestore(&ohci->lock, flags);
  1634. }
  1635. static int
  1636. ohci_queue_iso_transmit(struct fw_iso_context *base,
  1637. struct fw_iso_packet *packet,
  1638. struct fw_iso_buffer *buffer,
  1639. unsigned long payload)
  1640. {
  1641. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1642. struct descriptor *d, *last, *pd;
  1643. struct fw_iso_packet *p;
  1644. __le32 *header;
  1645. dma_addr_t d_bus, page_bus;
  1646. u32 z, header_z, payload_z, irq;
  1647. u32 payload_index, payload_end_index, next_page_index;
  1648. int page, end_page, i, length, offset;
  1649. /*
  1650. * FIXME: Cycle lost behavior should be configurable: lose
  1651. * packet, retransmit or terminate..
  1652. */
  1653. p = packet;
  1654. payload_index = payload;
  1655. if (p->skip)
  1656. z = 1;
  1657. else
  1658. z = 2;
  1659. if (p->header_length > 0)
  1660. z++;
  1661. /* Determine the first page the payload isn't contained in. */
  1662. end_page = PAGE_ALIGN(payload_index + p->payload_length) >> PAGE_SHIFT;
  1663. if (p->payload_length > 0)
  1664. payload_z = end_page - (payload_index >> PAGE_SHIFT);
  1665. else
  1666. payload_z = 0;
  1667. z += payload_z;
  1668. /* Get header size in number of descriptors. */
  1669. header_z = DIV_ROUND_UP(p->header_length, sizeof(*d));
  1670. d = context_get_descriptors(&ctx->context, z + header_z, &d_bus);
  1671. if (d == NULL)
  1672. return -ENOMEM;
  1673. if (!p->skip) {
  1674. d[0].control = cpu_to_le16(DESCRIPTOR_KEY_IMMEDIATE);
  1675. d[0].req_count = cpu_to_le16(8);
  1676. header = (__le32 *) &d[1];
  1677. header[0] = cpu_to_le32(IT_HEADER_SY(p->sy) |
  1678. IT_HEADER_TAG(p->tag) |
  1679. IT_HEADER_TCODE(TCODE_STREAM_DATA) |
  1680. IT_HEADER_CHANNEL(ctx->base.channel) |
  1681. IT_HEADER_SPEED(ctx->base.speed));
  1682. header[1] =
  1683. cpu_to_le32(IT_HEADER_DATA_LENGTH(p->header_length +
  1684. p->payload_length));
  1685. }
  1686. if (p->header_length > 0) {
  1687. d[2].req_count = cpu_to_le16(p->header_length);
  1688. d[2].data_address = cpu_to_le32(d_bus + z * sizeof(*d));
  1689. memcpy(&d[z], p->header, p->header_length);
  1690. }
  1691. pd = d + z - payload_z;
  1692. payload_end_index = payload_index + p->payload_length;
  1693. for (i = 0; i < payload_z; i++) {
  1694. page = payload_index >> PAGE_SHIFT;
  1695. offset = payload_index & ~PAGE_MASK;
  1696. next_page_index = (page + 1) << PAGE_SHIFT;
  1697. length =
  1698. min(next_page_index, payload_end_index) - payload_index;
  1699. pd[i].req_count = cpu_to_le16(length);
  1700. page_bus = page_private(buffer->pages[page]);
  1701. pd[i].data_address = cpu_to_le32(page_bus + offset);
  1702. payload_index += length;
  1703. }
  1704. if (p->interrupt)
  1705. irq = DESCRIPTOR_IRQ_ALWAYS;
  1706. else
  1707. irq = DESCRIPTOR_NO_IRQ;
  1708. last = z == 2 ? d : d + z - 1;
  1709. last->control |= cpu_to_le16(DESCRIPTOR_OUTPUT_LAST |
  1710. DESCRIPTOR_STATUS |
  1711. DESCRIPTOR_BRANCH_ALWAYS |
  1712. irq);
  1713. context_append(&ctx->context, d, z, header_z);
  1714. return 0;
  1715. }
  1716. static int
  1717. ohci_queue_iso_receive_dualbuffer(struct fw_iso_context *base,
  1718. struct fw_iso_packet *packet,
  1719. struct fw_iso_buffer *buffer,
  1720. unsigned long payload)
  1721. {
  1722. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1723. struct db_descriptor *db = NULL;
  1724. struct descriptor *d;
  1725. struct fw_iso_packet *p;
  1726. dma_addr_t d_bus, page_bus;
  1727. u32 z, header_z, length, rest;
  1728. int page, offset, packet_count, header_size;
  1729. /*
  1730. * FIXME: Cycle lost behavior should be configurable: lose
  1731. * packet, retransmit or terminate..
  1732. */
  1733. p = packet;
  1734. z = 2;
  1735. /*
  1736. * The OHCI controller puts the status word in the header
  1737. * buffer too, so we need 4 extra bytes per packet.
  1738. */
  1739. packet_count = p->header_length / ctx->base.header_size;
  1740. header_size = packet_count * (ctx->base.header_size + 4);
  1741. /* Get header size in number of descriptors. */
  1742. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  1743. page = payload >> PAGE_SHIFT;
  1744. offset = payload & ~PAGE_MASK;
  1745. rest = p->payload_length;
  1746. /* FIXME: make packet-per-buffer/dual-buffer a context option */
  1747. while (rest > 0) {
  1748. d = context_get_descriptors(&ctx->context,
  1749. z + header_z, &d_bus);
  1750. if (d == NULL)
  1751. return -ENOMEM;
  1752. db = (struct db_descriptor *) d;
  1753. db->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1754. DESCRIPTOR_BRANCH_ALWAYS);
  1755. db->first_size = cpu_to_le16(ctx->base.header_size + 4);
  1756. if (p->skip && rest == p->payload_length) {
  1757. db->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  1758. db->first_req_count = db->first_size;
  1759. } else {
  1760. db->first_req_count = cpu_to_le16(header_size);
  1761. }
  1762. db->first_res_count = db->first_req_count;
  1763. db->first_buffer = cpu_to_le32(d_bus + sizeof(*db));
  1764. if (p->skip && rest == p->payload_length)
  1765. length = 4;
  1766. else if (offset + rest < PAGE_SIZE)
  1767. length = rest;
  1768. else
  1769. length = PAGE_SIZE - offset;
  1770. db->second_req_count = cpu_to_le16(length);
  1771. db->second_res_count = db->second_req_count;
  1772. page_bus = page_private(buffer->pages[page]);
  1773. db->second_buffer = cpu_to_le32(page_bus + offset);
  1774. if (p->interrupt && length == rest)
  1775. db->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  1776. context_append(&ctx->context, d, z, header_z);
  1777. offset = (offset + length) & ~PAGE_MASK;
  1778. rest -= length;
  1779. if (offset == 0)
  1780. page++;
  1781. }
  1782. return 0;
  1783. }
  1784. static int
  1785. ohci_queue_iso_receive_packet_per_buffer(struct fw_iso_context *base,
  1786. struct fw_iso_packet *packet,
  1787. struct fw_iso_buffer *buffer,
  1788. unsigned long payload)
  1789. {
  1790. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1791. struct descriptor *d = NULL, *pd = NULL;
  1792. struct fw_iso_packet *p = packet;
  1793. dma_addr_t d_bus, page_bus;
  1794. u32 z, header_z, rest;
  1795. int i, j, length;
  1796. int page, offset, packet_count, header_size, payload_per_buffer;
  1797. /*
  1798. * The OHCI controller puts the status word in the
  1799. * buffer too, so we need 4 extra bytes per packet.
  1800. */
  1801. packet_count = p->header_length / ctx->base.header_size;
  1802. header_size = ctx->base.header_size + 4;
  1803. /* Get header size in number of descriptors. */
  1804. header_z = DIV_ROUND_UP(header_size, sizeof(*d));
  1805. page = payload >> PAGE_SHIFT;
  1806. offset = payload & ~PAGE_MASK;
  1807. payload_per_buffer = p->payload_length / packet_count;
  1808. for (i = 0; i < packet_count; i++) {
  1809. /* d points to the header descriptor */
  1810. z = DIV_ROUND_UP(payload_per_buffer + offset, PAGE_SIZE) + 1;
  1811. d = context_get_descriptors(&ctx->context,
  1812. z + header_z, &d_bus);
  1813. if (d == NULL)
  1814. return -ENOMEM;
  1815. d->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1816. DESCRIPTOR_INPUT_MORE);
  1817. if (p->skip && i == 0)
  1818. d->control |= cpu_to_le16(DESCRIPTOR_WAIT);
  1819. d->req_count = cpu_to_le16(header_size);
  1820. d->res_count = d->req_count;
  1821. d->transfer_status = 0;
  1822. d->data_address = cpu_to_le32(d_bus + (z * sizeof(*d)));
  1823. rest = payload_per_buffer;
  1824. for (j = 1; j < z; j++) {
  1825. pd = d + j;
  1826. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1827. DESCRIPTOR_INPUT_MORE);
  1828. if (offset + rest < PAGE_SIZE)
  1829. length = rest;
  1830. else
  1831. length = PAGE_SIZE - offset;
  1832. pd->req_count = cpu_to_le16(length);
  1833. pd->res_count = pd->req_count;
  1834. pd->transfer_status = 0;
  1835. page_bus = page_private(buffer->pages[page]);
  1836. pd->data_address = cpu_to_le32(page_bus + offset);
  1837. offset = (offset + length) & ~PAGE_MASK;
  1838. rest -= length;
  1839. if (offset == 0)
  1840. page++;
  1841. }
  1842. pd->control = cpu_to_le16(DESCRIPTOR_STATUS |
  1843. DESCRIPTOR_INPUT_LAST |
  1844. DESCRIPTOR_BRANCH_ALWAYS);
  1845. if (p->interrupt && i == packet_count - 1)
  1846. pd->control |= cpu_to_le16(DESCRIPTOR_IRQ_ALWAYS);
  1847. context_append(&ctx->context, d, z, header_z);
  1848. }
  1849. return 0;
  1850. }
  1851. static int
  1852. ohci_queue_iso(struct fw_iso_context *base,
  1853. struct fw_iso_packet *packet,
  1854. struct fw_iso_buffer *buffer,
  1855. unsigned long payload)
  1856. {
  1857. struct iso_context *ctx = container_of(base, struct iso_context, base);
  1858. unsigned long flags;
  1859. int retval;
  1860. spin_lock_irqsave(&ctx->context.ohci->lock, flags);
  1861. if (base->type == FW_ISO_CONTEXT_TRANSMIT)
  1862. retval = ohci_queue_iso_transmit(base, packet, buffer, payload);
  1863. else if (ctx->context.ohci->version >= OHCI_VERSION_1_1)
  1864. retval = ohci_queue_iso_receive_dualbuffer(base, packet,
  1865. buffer, payload);
  1866. else
  1867. retval = ohci_queue_iso_receive_packet_per_buffer(base, packet,
  1868. buffer,
  1869. payload);
  1870. spin_unlock_irqrestore(&ctx->context.ohci->lock, flags);
  1871. return retval;
  1872. }
  1873. static const struct fw_card_driver ohci_driver = {
  1874. .name = ohci_driver_name,
  1875. .enable = ohci_enable,
  1876. .update_phy_reg = ohci_update_phy_reg,
  1877. .set_config_rom = ohci_set_config_rom,
  1878. .send_request = ohci_send_request,
  1879. .send_response = ohci_send_response,
  1880. .cancel_packet = ohci_cancel_packet,
  1881. .enable_phys_dma = ohci_enable_phys_dma,
  1882. .get_bus_time = ohci_get_bus_time,
  1883. .allocate_iso_context = ohci_allocate_iso_context,
  1884. .free_iso_context = ohci_free_iso_context,
  1885. .queue_iso = ohci_queue_iso,
  1886. .start_iso = ohci_start_iso,
  1887. .stop_iso = ohci_stop_iso,
  1888. };
  1889. #ifdef CONFIG_PPC_PMAC
  1890. static void ohci_pmac_on(struct pci_dev *dev)
  1891. {
  1892. if (machine_is(powermac)) {
  1893. struct device_node *ofn = pci_device_to_OF_node(dev);
  1894. if (ofn) {
  1895. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 1);
  1896. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 1);
  1897. }
  1898. }
  1899. }
  1900. static void ohci_pmac_off(struct pci_dev *dev)
  1901. {
  1902. if (machine_is(powermac)) {
  1903. struct device_node *ofn = pci_device_to_OF_node(dev);
  1904. if (ofn) {
  1905. pmac_call_feature(PMAC_FTR_1394_ENABLE, ofn, 0, 0);
  1906. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, ofn, 0, 0);
  1907. }
  1908. }
  1909. }
  1910. #else
  1911. #define ohci_pmac_on(dev)
  1912. #define ohci_pmac_off(dev)
  1913. #endif /* CONFIG_PPC_PMAC */
  1914. static int __devinit
  1915. pci_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  1916. {
  1917. struct fw_ohci *ohci;
  1918. u32 bus_options, max_receive, link_speed;
  1919. u64 guid;
  1920. int err;
  1921. size_t size;
  1922. ohci_pmac_on(dev);
  1923. ohci = kzalloc(sizeof(*ohci), GFP_KERNEL);
  1924. if (ohci == NULL) {
  1925. fw_error("Could not malloc fw_ohci data.\n");
  1926. return -ENOMEM;
  1927. }
  1928. fw_card_initialize(&ohci->card, &ohci_driver, &dev->dev);
  1929. err = pci_enable_device(dev);
  1930. if (err) {
  1931. fw_error("Failed to enable OHCI hardware.\n");
  1932. goto fail_free;
  1933. }
  1934. pci_set_master(dev);
  1935. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  1936. pci_set_drvdata(dev, ohci);
  1937. #if defined(CONFIG_PPC_PMAC) && defined(CONFIG_PPC32)
  1938. ohci->old_uninorth = dev->vendor == PCI_VENDOR_ID_APPLE &&
  1939. dev->device == PCI_DEVICE_ID_APPLE_UNI_N_FW;
  1940. #endif
  1941. spin_lock_init(&ohci->lock);
  1942. tasklet_init(&ohci->bus_reset_tasklet,
  1943. bus_reset_tasklet, (unsigned long)ohci);
  1944. err = pci_request_region(dev, 0, ohci_driver_name);
  1945. if (err) {
  1946. fw_error("MMIO resource unavailable\n");
  1947. goto fail_disable;
  1948. }
  1949. ohci->registers = pci_iomap(dev, 0, OHCI1394_REGISTER_SIZE);
  1950. if (ohci->registers == NULL) {
  1951. fw_error("Failed to remap registers\n");
  1952. err = -ENXIO;
  1953. goto fail_iomem;
  1954. }
  1955. ar_context_init(&ohci->ar_request_ctx, ohci,
  1956. OHCI1394_AsReqRcvContextControlSet);
  1957. ar_context_init(&ohci->ar_response_ctx, ohci,
  1958. OHCI1394_AsRspRcvContextControlSet);
  1959. context_init(&ohci->at_request_ctx, ohci,
  1960. OHCI1394_AsReqTrContextControlSet, handle_at_packet);
  1961. context_init(&ohci->at_response_ctx, ohci,
  1962. OHCI1394_AsRspTrContextControlSet, handle_at_packet);
  1963. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, ~0);
  1964. ohci->it_context_mask = reg_read(ohci, OHCI1394_IsoRecvIntMaskSet);
  1965. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, ~0);
  1966. size = sizeof(struct iso_context) * hweight32(ohci->it_context_mask);
  1967. ohci->it_context_list = kzalloc(size, GFP_KERNEL);
  1968. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, ~0);
  1969. ohci->ir_context_mask = reg_read(ohci, OHCI1394_IsoXmitIntMaskSet);
  1970. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, ~0);
  1971. size = sizeof(struct iso_context) * hweight32(ohci->ir_context_mask);
  1972. ohci->ir_context_list = kzalloc(size, GFP_KERNEL);
  1973. if (ohci->it_context_list == NULL || ohci->ir_context_list == NULL) {
  1974. fw_error("Out of memory for it/ir contexts.\n");
  1975. err = -ENOMEM;
  1976. goto fail_registers;
  1977. }
  1978. /* self-id dma buffer allocation */
  1979. ohci->self_id_cpu = dma_alloc_coherent(ohci->card.device,
  1980. SELF_ID_BUF_SIZE,
  1981. &ohci->self_id_bus,
  1982. GFP_KERNEL);
  1983. if (ohci->self_id_cpu == NULL) {
  1984. fw_error("Out of memory for self ID buffer.\n");
  1985. err = -ENOMEM;
  1986. goto fail_registers;
  1987. }
  1988. bus_options = reg_read(ohci, OHCI1394_BusOptions);
  1989. max_receive = (bus_options >> 12) & 0xf;
  1990. link_speed = bus_options & 0x7;
  1991. guid = ((u64) reg_read(ohci, OHCI1394_GUIDHi) << 32) |
  1992. reg_read(ohci, OHCI1394_GUIDLo);
  1993. err = fw_card_add(&ohci->card, max_receive, link_speed, guid);
  1994. if (err < 0)
  1995. goto fail_self_id;
  1996. ohci->version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
  1997. fw_notify("Added fw-ohci device %s, OHCI version %x.%x\n",
  1998. dev->dev.bus_id, ohci->version >> 16, ohci->version & 0xff);
  1999. return 0;
  2000. fail_self_id:
  2001. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  2002. ohci->self_id_cpu, ohci->self_id_bus);
  2003. fail_registers:
  2004. kfree(ohci->it_context_list);
  2005. kfree(ohci->ir_context_list);
  2006. pci_iounmap(dev, ohci->registers);
  2007. fail_iomem:
  2008. pci_release_region(dev, 0);
  2009. fail_disable:
  2010. pci_disable_device(dev);
  2011. fail_free:
  2012. kfree(&ohci->card);
  2013. return err;
  2014. }
  2015. static void pci_remove(struct pci_dev *dev)
  2016. {
  2017. struct fw_ohci *ohci;
  2018. ohci = pci_get_drvdata(dev);
  2019. reg_write(ohci, OHCI1394_IntMaskClear, ~0);
  2020. flush_writes(ohci);
  2021. fw_core_remove_card(&ohci->card);
  2022. /*
  2023. * FIXME: Fail all pending packets here, now that the upper
  2024. * layers can't queue any more.
  2025. */
  2026. software_reset(ohci);
  2027. free_irq(dev->irq, ohci);
  2028. dma_free_coherent(ohci->card.device, SELF_ID_BUF_SIZE,
  2029. ohci->self_id_cpu, ohci->self_id_bus);
  2030. kfree(ohci->it_context_list);
  2031. kfree(ohci->ir_context_list);
  2032. pci_iounmap(dev, ohci->registers);
  2033. pci_release_region(dev, 0);
  2034. pci_disable_device(dev);
  2035. kfree(&ohci->card);
  2036. ohci_pmac_off(dev);
  2037. fw_notify("Removed fw-ohci device.\n");
  2038. }
  2039. #ifdef CONFIG_PM
  2040. static int pci_suspend(struct pci_dev *dev, pm_message_t state)
  2041. {
  2042. struct fw_ohci *ohci = pci_get_drvdata(dev);
  2043. int err;
  2044. software_reset(ohci);
  2045. free_irq(dev->irq, ohci);
  2046. err = pci_save_state(dev);
  2047. if (err) {
  2048. fw_error("pci_save_state failed\n");
  2049. return err;
  2050. }
  2051. err = pci_set_power_state(dev, pci_choose_state(dev, state));
  2052. if (err)
  2053. fw_error("pci_set_power_state failed with %d\n", err);
  2054. ohci_pmac_off(dev);
  2055. return 0;
  2056. }
  2057. static int pci_resume(struct pci_dev *dev)
  2058. {
  2059. struct fw_ohci *ohci = pci_get_drvdata(dev);
  2060. int err;
  2061. ohci_pmac_on(dev);
  2062. pci_set_power_state(dev, PCI_D0);
  2063. pci_restore_state(dev);
  2064. err = pci_enable_device(dev);
  2065. if (err) {
  2066. fw_error("pci_enable_device failed\n");
  2067. return err;
  2068. }
  2069. return ohci_enable(&ohci->card, NULL, 0);
  2070. }
  2071. #endif
  2072. static struct pci_device_id pci_table[] = {
  2073. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_FIREWIRE_OHCI, ~0) },
  2074. { }
  2075. };
  2076. MODULE_DEVICE_TABLE(pci, pci_table);
  2077. static struct pci_driver fw_ohci_pci_driver = {
  2078. .name = ohci_driver_name,
  2079. .id_table = pci_table,
  2080. .probe = pci_probe,
  2081. .remove = pci_remove,
  2082. #ifdef CONFIG_PM
  2083. .resume = pci_resume,
  2084. .suspend = pci_suspend,
  2085. #endif
  2086. };
  2087. MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
  2088. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE1394 controllers");
  2089. MODULE_LICENSE("GPL");
  2090. /* Provide a module alias so root-on-sbp2 initrds don't break. */
  2091. #ifndef CONFIG_IEEE1394_OHCI1394_MODULE
  2092. MODULE_ALIAS("ohci1394");
  2093. #endif
  2094. static int __init fw_ohci_init(void)
  2095. {
  2096. return pci_register_driver(&fw_ohci_pci_driver);
  2097. }
  2098. static void __exit fw_ohci_cleanup(void)
  2099. {
  2100. pci_unregister_driver(&fw_ohci_pci_driver);
  2101. }
  2102. module_init(fw_ohci_init);
  2103. module_exit(fw_ohci_cleanup);