ixp4xx_hss.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /*
  2. * Intel IXP4xx HSS (synchronous serial port) driver for Linux
  3. *
  4. * Copyright (C) 2007-2008 Krzysztof Hałasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/bitops.h>
  12. #include <linux/cdev.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dmapool.h>
  15. #include <linux/fs.h>
  16. #include <linux/hdlc.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/poll.h>
  21. #include <linux/slab.h>
  22. #include <mach/npe.h>
  23. #include <mach/qmgr.h>
  24. #define DEBUG_DESC 0
  25. #define DEBUG_RX 0
  26. #define DEBUG_TX 0
  27. #define DEBUG_PKT_BYTES 0
  28. #define DEBUG_CLOSE 0
  29. #define DRV_NAME "ixp4xx_hss"
  30. #define PKT_EXTRA_FLAGS 0 /* orig 1 */
  31. #define PKT_NUM_PIPES 1 /* 1, 2 or 4 */
  32. #define PKT_PIPE_FIFO_SIZEW 4 /* total 4 dwords per HSS */
  33. #define RX_DESCS 16 /* also length of all RX queues */
  34. #define TX_DESCS 16 /* also length of all TX queues */
  35. #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
  36. #define RX_SIZE (HDLC_MAX_MRU + 4) /* NPE needs more space */
  37. #define MAX_CLOSE_WAIT 1000 /* microseconds */
  38. #define HSS_COUNT 2
  39. #define FRAME_SIZE 256 /* doesn't matter at this point */
  40. #define FRAME_OFFSET 0
  41. #define MAX_CHANNELS (FRAME_SIZE / 8)
  42. #define NAPI_WEIGHT 16
  43. /* Queue IDs */
  44. #define HSS0_CHL_RXTRIG_QUEUE 12 /* orig size = 32 dwords */
  45. #define HSS0_PKT_RX_QUEUE 13 /* orig size = 32 dwords */
  46. #define HSS0_PKT_TX0_QUEUE 14 /* orig size = 16 dwords */
  47. #define HSS0_PKT_TX1_QUEUE 15
  48. #define HSS0_PKT_TX2_QUEUE 16
  49. #define HSS0_PKT_TX3_QUEUE 17
  50. #define HSS0_PKT_RXFREE0_QUEUE 18 /* orig size = 16 dwords */
  51. #define HSS0_PKT_RXFREE1_QUEUE 19
  52. #define HSS0_PKT_RXFREE2_QUEUE 20
  53. #define HSS0_PKT_RXFREE3_QUEUE 21
  54. #define HSS0_PKT_TXDONE_QUEUE 22 /* orig size = 64 dwords */
  55. #define HSS1_CHL_RXTRIG_QUEUE 10
  56. #define HSS1_PKT_RX_QUEUE 0
  57. #define HSS1_PKT_TX0_QUEUE 5
  58. #define HSS1_PKT_TX1_QUEUE 6
  59. #define HSS1_PKT_TX2_QUEUE 7
  60. #define HSS1_PKT_TX3_QUEUE 8
  61. #define HSS1_PKT_RXFREE0_QUEUE 1
  62. #define HSS1_PKT_RXFREE1_QUEUE 2
  63. #define HSS1_PKT_RXFREE2_QUEUE 3
  64. #define HSS1_PKT_RXFREE3_QUEUE 4
  65. #define HSS1_PKT_TXDONE_QUEUE 9
  66. #define NPE_PKT_MODE_HDLC 0
  67. #define NPE_PKT_MODE_RAW 1
  68. #define NPE_PKT_MODE_56KMODE 2
  69. #define NPE_PKT_MODE_56KENDIAN_MSB 4
  70. /* PKT_PIPE_HDLC_CFG_WRITE flags */
  71. #define PKT_HDLC_IDLE_ONES 0x1 /* default = flags */
  72. #define PKT_HDLC_CRC_32 0x2 /* default = CRC-16 */
  73. #define PKT_HDLC_MSB_ENDIAN 0x4 /* default = LE */
  74. /* hss_config, PCRs */
  75. /* Frame sync sampling, default = active low */
  76. #define PCR_FRM_SYNC_ACTIVE_HIGH 0x40000000
  77. #define PCR_FRM_SYNC_FALLINGEDGE 0x80000000
  78. #define PCR_FRM_SYNC_RISINGEDGE 0xC0000000
  79. /* Frame sync pin: input (default) or output generated off a given clk edge */
  80. #define PCR_FRM_SYNC_OUTPUT_FALLING 0x20000000
  81. #define PCR_FRM_SYNC_OUTPUT_RISING 0x30000000
  82. /* Frame and data clock sampling on edge, default = falling */
  83. #define PCR_FCLK_EDGE_RISING 0x08000000
  84. #define PCR_DCLK_EDGE_RISING 0x04000000
  85. /* Clock direction, default = input */
  86. #define PCR_SYNC_CLK_DIR_OUTPUT 0x02000000
  87. /* Generate/Receive frame pulses, default = enabled */
  88. #define PCR_FRM_PULSE_DISABLED 0x01000000
  89. /* Data rate is full (default) or half the configured clk speed */
  90. #define PCR_HALF_CLK_RATE 0x00200000
  91. /* Invert data between NPE and HSS FIFOs? (default = no) */
  92. #define PCR_DATA_POLARITY_INVERT 0x00100000
  93. /* TX/RX endianness, default = LSB */
  94. #define PCR_MSB_ENDIAN 0x00080000
  95. /* Normal (default) / open drain mode (TX only) */
  96. #define PCR_TX_PINS_OPEN_DRAIN 0x00040000
  97. /* No framing bit transmitted and expected on RX? (default = framing bit) */
  98. #define PCR_SOF_NO_FBIT 0x00020000
  99. /* Drive data pins? */
  100. #define PCR_TX_DATA_ENABLE 0x00010000
  101. /* Voice 56k type: drive the data pins low (default), high, high Z */
  102. #define PCR_TX_V56K_HIGH 0x00002000
  103. #define PCR_TX_V56K_HIGH_IMP 0x00004000
  104. /* Unassigned type: drive the data pins low (default), high, high Z */
  105. #define PCR_TX_UNASS_HIGH 0x00000800
  106. #define PCR_TX_UNASS_HIGH_IMP 0x00001000
  107. /* T1 @ 1.544MHz only: Fbit dictated in FIFO (default) or high Z */
  108. #define PCR_TX_FB_HIGH_IMP 0x00000400
  109. /* 56k data endiannes - which bit unused: high (default) or low */
  110. #define PCR_TX_56KE_BIT_0_UNUSED 0x00000200
  111. /* 56k data transmission type: 32/8 bit data (default) or 56K data */
  112. #define PCR_TX_56KS_56K_DATA 0x00000100
  113. /* hss_config, cCR */
  114. /* Number of packetized clients, default = 1 */
  115. #define CCR_NPE_HFIFO_2_HDLC 0x04000000
  116. #define CCR_NPE_HFIFO_3_OR_4HDLC 0x08000000
  117. /* default = no loopback */
  118. #define CCR_LOOPBACK 0x02000000
  119. /* HSS number, default = 0 (first) */
  120. #define CCR_SECOND_HSS 0x01000000
  121. /* hss_config, clkCR: main:10, num:10, denom:12 */
  122. #define CLK42X_SPEED_EXP ((0x3FF << 22) | ( 2 << 12) | 15) /*65 KHz*/
  123. #define CLK42X_SPEED_512KHZ (( 130 << 22) | ( 2 << 12) | 15)
  124. #define CLK42X_SPEED_1536KHZ (( 43 << 22) | ( 18 << 12) | 47)
  125. #define CLK42X_SPEED_1544KHZ (( 43 << 22) | ( 33 << 12) | 192)
  126. #define CLK42X_SPEED_2048KHZ (( 32 << 22) | ( 34 << 12) | 63)
  127. #define CLK42X_SPEED_4096KHZ (( 16 << 22) | ( 34 << 12) | 127)
  128. #define CLK42X_SPEED_8192KHZ (( 8 << 22) | ( 34 << 12) | 255)
  129. #define CLK46X_SPEED_512KHZ (( 130 << 22) | ( 24 << 12) | 127)
  130. #define CLK46X_SPEED_1536KHZ (( 43 << 22) | (152 << 12) | 383)
  131. #define CLK46X_SPEED_1544KHZ (( 43 << 22) | ( 66 << 12) | 385)
  132. #define CLK46X_SPEED_2048KHZ (( 32 << 22) | (280 << 12) | 511)
  133. #define CLK46X_SPEED_4096KHZ (( 16 << 22) | (280 << 12) | 1023)
  134. #define CLK46X_SPEED_8192KHZ (( 8 << 22) | (280 << 12) | 2047)
  135. /*
  136. * HSS_CONFIG_CLOCK_CR register consists of 3 parts:
  137. * A (10 bits), B (10 bits) and C (12 bits).
  138. * IXP42x HSS clock generator operation (verified with an oscilloscope):
  139. * Each clock bit takes 7.5 ns (1 / 133.xx MHz).
  140. * The clock sequence consists of (C - B) states of 0s and 1s, each state is
  141. * A bits wide. It's followed by (B + 1) states of 0s and 1s, each state is
  142. * (A + 1) bits wide.
  143. *
  144. * The resulting average clock frequency (assuming 33.333 MHz oscillator) is:
  145. * freq = 66.666 MHz / (A + (B + 1) / (C + 1))
  146. * minimum freq = 66.666 MHz / (A + 1)
  147. * maximum freq = 66.666 MHz / A
  148. *
  149. * Example: A = 2, B = 2, C = 7, CLOCK_CR register = 2 << 22 | 2 << 12 | 7
  150. * freq = 66.666 MHz / (2 + (2 + 1) / (7 + 1)) = 28.07 MHz (Mb/s).
  151. * The clock sequence is: 1100110011 (5 doubles) 000111000 (3 triples).
  152. * The sequence takes (C - B) * A + (B + 1) * (A + 1) = 5 * 2 + 3 * 3 bits
  153. * = 19 bits (each 7.5 ns long) = 142.5 ns (then the sequence repeats).
  154. * The sequence consists of 4 complete clock periods, thus the average
  155. * frequency (= clock rate) is 4 / 142.5 ns = 28.07 MHz (Mb/s).
  156. * (max specified clock rate for IXP42x HSS is 8.192 Mb/s).
  157. */
  158. /* hss_config, LUT entries */
  159. #define TDMMAP_UNASSIGNED 0
  160. #define TDMMAP_HDLC 1 /* HDLC - packetized */
  161. #define TDMMAP_VOICE56K 2 /* Voice56K - 7-bit channelized */
  162. #define TDMMAP_VOICE64K 3 /* Voice64K - 8-bit channelized */
  163. /* offsets into HSS config */
  164. #define HSS_CONFIG_TX_PCR 0x00 /* port configuration registers */
  165. #define HSS_CONFIG_RX_PCR 0x04
  166. #define HSS_CONFIG_CORE_CR 0x08 /* loopback control, HSS# */
  167. #define HSS_CONFIG_CLOCK_CR 0x0C /* clock generator control */
  168. #define HSS_CONFIG_TX_FCR 0x10 /* frame configuration registers */
  169. #define HSS_CONFIG_RX_FCR 0x14
  170. #define HSS_CONFIG_TX_LUT 0x18 /* channel look-up tables */
  171. #define HSS_CONFIG_RX_LUT 0x38
  172. /* NPE command codes */
  173. /* writes the ConfigWord value to the location specified by offset */
  174. #define PORT_CONFIG_WRITE 0x40
  175. /* triggers the NPE to load the contents of the configuration table */
  176. #define PORT_CONFIG_LOAD 0x41
  177. /* triggers the NPE to return an HssErrorReadResponse message */
  178. #define PORT_ERROR_READ 0x42
  179. /* triggers the NPE to reset internal status and enable the HssPacketized
  180. operation for the flow specified by pPipe */
  181. #define PKT_PIPE_FLOW_ENABLE 0x50
  182. #define PKT_PIPE_FLOW_DISABLE 0x51
  183. #define PKT_NUM_PIPES_WRITE 0x52
  184. #define PKT_PIPE_FIFO_SIZEW_WRITE 0x53
  185. #define PKT_PIPE_HDLC_CFG_WRITE 0x54
  186. #define PKT_PIPE_IDLE_PATTERN_WRITE 0x55
  187. #define PKT_PIPE_RX_SIZE_WRITE 0x56
  188. #define PKT_PIPE_MODE_WRITE 0x57
  189. /* HDLC packet status values - desc->status */
  190. #define ERR_SHUTDOWN 1 /* stop or shutdown occurrence */
  191. #define ERR_HDLC_ALIGN 2 /* HDLC alignment error */
  192. #define ERR_HDLC_FCS 3 /* HDLC Frame Check Sum error */
  193. #define ERR_RXFREE_Q_EMPTY 4 /* RX-free queue became empty while receiving
  194. this packet (if buf_len < pkt_len) */
  195. #define ERR_HDLC_TOO_LONG 5 /* HDLC frame size too long */
  196. #define ERR_HDLC_ABORT 6 /* abort sequence received */
  197. #define ERR_DISCONNECTING 7 /* disconnect is in progress */
  198. #ifdef __ARMEB__
  199. typedef struct sk_buff buffer_t;
  200. #define free_buffer dev_kfree_skb
  201. #define free_buffer_irq dev_kfree_skb_irq
  202. #else
  203. typedef void buffer_t;
  204. #define free_buffer kfree
  205. #define free_buffer_irq kfree
  206. #endif
  207. struct port {
  208. struct device *dev;
  209. struct npe *npe;
  210. struct net_device *netdev;
  211. struct napi_struct napi;
  212. struct hss_plat_info *plat;
  213. buffer_t *rx_buff_tab[RX_DESCS], *tx_buff_tab[TX_DESCS];
  214. struct desc *desc_tab; /* coherent */
  215. u32 desc_tab_phys;
  216. unsigned int id;
  217. unsigned int clock_type, clock_rate, loopback;
  218. unsigned int initialized, carrier;
  219. u8 hdlc_cfg;
  220. u32 clock_reg;
  221. };
  222. /* NPE message structure */
  223. struct msg {
  224. #ifdef __ARMEB__
  225. u8 cmd, unused, hss_port, index;
  226. union {
  227. struct { u8 data8a, data8b, data8c, data8d; };
  228. struct { u16 data16a, data16b; };
  229. struct { u32 data32; };
  230. };
  231. #else
  232. u8 index, hss_port, unused, cmd;
  233. union {
  234. struct { u8 data8d, data8c, data8b, data8a; };
  235. struct { u16 data16b, data16a; };
  236. struct { u32 data32; };
  237. };
  238. #endif
  239. };
  240. /* HDLC packet descriptor */
  241. struct desc {
  242. u32 next; /* pointer to next buffer, unused */
  243. #ifdef __ARMEB__
  244. u16 buf_len; /* buffer length */
  245. u16 pkt_len; /* packet length */
  246. u32 data; /* pointer to data buffer in RAM */
  247. u8 status;
  248. u8 error_count;
  249. u16 __reserved;
  250. #else
  251. u16 pkt_len; /* packet length */
  252. u16 buf_len; /* buffer length */
  253. u32 data; /* pointer to data buffer in RAM */
  254. u16 __reserved;
  255. u8 error_count;
  256. u8 status;
  257. #endif
  258. u32 __reserved1[4];
  259. };
  260. #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
  261. (n) * sizeof(struct desc))
  262. #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
  263. #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
  264. ((n) + RX_DESCS) * sizeof(struct desc))
  265. #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
  266. /*****************************************************************************
  267. * global variables
  268. ****************************************************************************/
  269. static int ports_open;
  270. static struct dma_pool *dma_pool;
  271. static spinlock_t npe_lock;
  272. static const struct {
  273. int tx, txdone, rx, rxfree;
  274. }queue_ids[2] = {{HSS0_PKT_TX0_QUEUE, HSS0_PKT_TXDONE_QUEUE, HSS0_PKT_RX_QUEUE,
  275. HSS0_PKT_RXFREE0_QUEUE},
  276. {HSS1_PKT_TX0_QUEUE, HSS1_PKT_TXDONE_QUEUE, HSS1_PKT_RX_QUEUE,
  277. HSS1_PKT_RXFREE0_QUEUE},
  278. };
  279. /*****************************************************************************
  280. * utility functions
  281. ****************************************************************************/
  282. static inline struct port* dev_to_port(struct net_device *dev)
  283. {
  284. return dev_to_hdlc(dev)->priv;
  285. }
  286. #ifndef __ARMEB__
  287. static inline void memcpy_swab32(u32 *dest, u32 *src, int cnt)
  288. {
  289. int i;
  290. for (i = 0; i < cnt; i++)
  291. dest[i] = swab32(src[i]);
  292. }
  293. #endif
  294. /*****************************************************************************
  295. * HSS access
  296. ****************************************************************************/
  297. static void hss_npe_send(struct port *port, struct msg *msg, const char* what)
  298. {
  299. u32 *val = (u32*)msg;
  300. if (npe_send_message(port->npe, msg, what)) {
  301. pr_crit("HSS-%i: unable to send command [%08X:%08X] to %s\n",
  302. port->id, val[0], val[1], npe_name(port->npe));
  303. BUG();
  304. }
  305. }
  306. static void hss_config_set_lut(struct port *port)
  307. {
  308. struct msg msg;
  309. int ch;
  310. memset(&msg, 0, sizeof(msg));
  311. msg.cmd = PORT_CONFIG_WRITE;
  312. msg.hss_port = port->id;
  313. for (ch = 0; ch < MAX_CHANNELS; ch++) {
  314. msg.data32 >>= 2;
  315. msg.data32 |= TDMMAP_HDLC << 30;
  316. if (ch % 16 == 15) {
  317. msg.index = HSS_CONFIG_TX_LUT + ((ch / 4) & ~3);
  318. hss_npe_send(port, &msg, "HSS_SET_TX_LUT");
  319. msg.index += HSS_CONFIG_RX_LUT - HSS_CONFIG_TX_LUT;
  320. hss_npe_send(port, &msg, "HSS_SET_RX_LUT");
  321. }
  322. }
  323. }
  324. static void hss_config(struct port *port)
  325. {
  326. struct msg msg;
  327. memset(&msg, 0, sizeof(msg));
  328. msg.cmd = PORT_CONFIG_WRITE;
  329. msg.hss_port = port->id;
  330. msg.index = HSS_CONFIG_TX_PCR;
  331. msg.data32 = PCR_FRM_PULSE_DISABLED | PCR_MSB_ENDIAN |
  332. PCR_TX_DATA_ENABLE | PCR_SOF_NO_FBIT;
  333. if (port->clock_type == CLOCK_INT)
  334. msg.data32 |= PCR_SYNC_CLK_DIR_OUTPUT;
  335. hss_npe_send(port, &msg, "HSS_SET_TX_PCR");
  336. msg.index = HSS_CONFIG_RX_PCR;
  337. msg.data32 ^= PCR_TX_DATA_ENABLE | PCR_DCLK_EDGE_RISING;
  338. hss_npe_send(port, &msg, "HSS_SET_RX_PCR");
  339. memset(&msg, 0, sizeof(msg));
  340. msg.cmd = PORT_CONFIG_WRITE;
  341. msg.hss_port = port->id;
  342. msg.index = HSS_CONFIG_CORE_CR;
  343. msg.data32 = (port->loopback ? CCR_LOOPBACK : 0) |
  344. (port->id ? CCR_SECOND_HSS : 0);
  345. hss_npe_send(port, &msg, "HSS_SET_CORE_CR");
  346. memset(&msg, 0, sizeof(msg));
  347. msg.cmd = PORT_CONFIG_WRITE;
  348. msg.hss_port = port->id;
  349. msg.index = HSS_CONFIG_CLOCK_CR;
  350. msg.data32 = port->clock_reg;
  351. hss_npe_send(port, &msg, "HSS_SET_CLOCK_CR");
  352. memset(&msg, 0, sizeof(msg));
  353. msg.cmd = PORT_CONFIG_WRITE;
  354. msg.hss_port = port->id;
  355. msg.index = HSS_CONFIG_TX_FCR;
  356. msg.data16a = FRAME_OFFSET;
  357. msg.data16b = FRAME_SIZE - 1;
  358. hss_npe_send(port, &msg, "HSS_SET_TX_FCR");
  359. memset(&msg, 0, sizeof(msg));
  360. msg.cmd = PORT_CONFIG_WRITE;
  361. msg.hss_port = port->id;
  362. msg.index = HSS_CONFIG_RX_FCR;
  363. msg.data16a = FRAME_OFFSET;
  364. msg.data16b = FRAME_SIZE - 1;
  365. hss_npe_send(port, &msg, "HSS_SET_RX_FCR");
  366. hss_config_set_lut(port);
  367. memset(&msg, 0, sizeof(msg));
  368. msg.cmd = PORT_CONFIG_LOAD;
  369. msg.hss_port = port->id;
  370. hss_npe_send(port, &msg, "HSS_LOAD_CONFIG");
  371. if (npe_recv_message(port->npe, &msg, "HSS_LOAD_CONFIG") ||
  372. /* HSS_LOAD_CONFIG for port #1 returns port_id = #4 */
  373. msg.cmd != PORT_CONFIG_LOAD || msg.data32) {
  374. pr_crit("HSS-%i: HSS_LOAD_CONFIG failed\n", port->id);
  375. BUG();
  376. }
  377. /* HDLC may stop working without this - check FIXME */
  378. npe_recv_message(port->npe, &msg, "FLUSH_IT");
  379. }
  380. static void hss_set_hdlc_cfg(struct port *port)
  381. {
  382. struct msg msg;
  383. memset(&msg, 0, sizeof(msg));
  384. msg.cmd = PKT_PIPE_HDLC_CFG_WRITE;
  385. msg.hss_port = port->id;
  386. msg.data8a = port->hdlc_cfg; /* rx_cfg */
  387. msg.data8b = port->hdlc_cfg | (PKT_EXTRA_FLAGS << 3); /* tx_cfg */
  388. hss_npe_send(port, &msg, "HSS_SET_HDLC_CFG");
  389. }
  390. static u32 hss_get_status(struct port *port)
  391. {
  392. struct msg msg;
  393. memset(&msg, 0, sizeof(msg));
  394. msg.cmd = PORT_ERROR_READ;
  395. msg.hss_port = port->id;
  396. hss_npe_send(port, &msg, "PORT_ERROR_READ");
  397. if (npe_recv_message(port->npe, &msg, "PORT_ERROR_READ")) {
  398. pr_crit("HSS-%i: unable to read HSS status\n", port->id);
  399. BUG();
  400. }
  401. return msg.data32;
  402. }
  403. static void hss_start_hdlc(struct port *port)
  404. {
  405. struct msg msg;
  406. memset(&msg, 0, sizeof(msg));
  407. msg.cmd = PKT_PIPE_FLOW_ENABLE;
  408. msg.hss_port = port->id;
  409. msg.data32 = 0;
  410. hss_npe_send(port, &msg, "HSS_ENABLE_PKT_PIPE");
  411. }
  412. static void hss_stop_hdlc(struct port *port)
  413. {
  414. struct msg msg;
  415. memset(&msg, 0, sizeof(msg));
  416. msg.cmd = PKT_PIPE_FLOW_DISABLE;
  417. msg.hss_port = port->id;
  418. hss_npe_send(port, &msg, "HSS_DISABLE_PKT_PIPE");
  419. hss_get_status(port); /* make sure it's halted */
  420. }
  421. static int hss_load_firmware(struct port *port)
  422. {
  423. struct msg msg;
  424. int err;
  425. if (port->initialized)
  426. return 0;
  427. if (!npe_running(port->npe) &&
  428. (err = npe_load_firmware(port->npe, npe_name(port->npe),
  429. port->dev)))
  430. return err;
  431. /* HDLC mode configuration */
  432. memset(&msg, 0, sizeof(msg));
  433. msg.cmd = PKT_NUM_PIPES_WRITE;
  434. msg.hss_port = port->id;
  435. msg.data8a = PKT_NUM_PIPES;
  436. hss_npe_send(port, &msg, "HSS_SET_PKT_PIPES");
  437. msg.cmd = PKT_PIPE_FIFO_SIZEW_WRITE;
  438. msg.data8a = PKT_PIPE_FIFO_SIZEW;
  439. hss_npe_send(port, &msg, "HSS_SET_PKT_FIFO");
  440. msg.cmd = PKT_PIPE_MODE_WRITE;
  441. msg.data8a = NPE_PKT_MODE_HDLC;
  442. /* msg.data8b = inv_mask */
  443. /* msg.data8c = or_mask */
  444. hss_npe_send(port, &msg, "HSS_SET_PKT_MODE");
  445. msg.cmd = PKT_PIPE_RX_SIZE_WRITE;
  446. msg.data16a = HDLC_MAX_MRU; /* including CRC */
  447. hss_npe_send(port, &msg, "HSS_SET_PKT_RX_SIZE");
  448. msg.cmd = PKT_PIPE_IDLE_PATTERN_WRITE;
  449. msg.data32 = 0x7F7F7F7F; /* ??? FIXME */
  450. hss_npe_send(port, &msg, "HSS_SET_PKT_IDLE");
  451. port->initialized = 1;
  452. return 0;
  453. }
  454. /*****************************************************************************
  455. * packetized (HDLC) operation
  456. ****************************************************************************/
  457. static inline void debug_pkt(struct net_device *dev, const char *func,
  458. u8 *data, int len)
  459. {
  460. #if DEBUG_PKT_BYTES
  461. int i;
  462. printk(KERN_DEBUG "%s: %s(%i)", dev->name, func, len);
  463. for (i = 0; i < len; i++) {
  464. if (i >= DEBUG_PKT_BYTES)
  465. break;
  466. printk("%s%02X", !(i % 4) ? " " : "", data[i]);
  467. }
  468. printk("\n");
  469. #endif
  470. }
  471. static inline void debug_desc(u32 phys, struct desc *desc)
  472. {
  473. #if DEBUG_DESC
  474. printk(KERN_DEBUG "%X: %X %3X %3X %08X %X %X\n",
  475. phys, desc->next, desc->buf_len, desc->pkt_len,
  476. desc->data, desc->status, desc->error_count);
  477. #endif
  478. }
  479. static inline int queue_get_desc(unsigned int queue, struct port *port,
  480. int is_tx)
  481. {
  482. u32 phys, tab_phys, n_desc;
  483. struct desc *tab;
  484. if (!(phys = qmgr_get_entry(queue)))
  485. return -1;
  486. BUG_ON(phys & 0x1F);
  487. tab_phys = is_tx ? tx_desc_phys(port, 0) : rx_desc_phys(port, 0);
  488. tab = is_tx ? tx_desc_ptr(port, 0) : rx_desc_ptr(port, 0);
  489. n_desc = (phys - tab_phys) / sizeof(struct desc);
  490. BUG_ON(n_desc >= (is_tx ? TX_DESCS : RX_DESCS));
  491. debug_desc(phys, &tab[n_desc]);
  492. BUG_ON(tab[n_desc].next);
  493. return n_desc;
  494. }
  495. static inline void queue_put_desc(unsigned int queue, u32 phys,
  496. struct desc *desc)
  497. {
  498. debug_desc(phys, desc);
  499. BUG_ON(phys & 0x1F);
  500. qmgr_put_entry(queue, phys);
  501. /* Don't check for queue overflow here, we've allocated sufficient
  502. length and queues >= 32 don't support this check anyway. */
  503. }
  504. static inline void dma_unmap_tx(struct port *port, struct desc *desc)
  505. {
  506. #ifdef __ARMEB__
  507. dma_unmap_single(&port->netdev->dev, desc->data,
  508. desc->buf_len, DMA_TO_DEVICE);
  509. #else
  510. dma_unmap_single(&port->netdev->dev, desc->data & ~3,
  511. ALIGN((desc->data & 3) + desc->buf_len, 4),
  512. DMA_TO_DEVICE);
  513. #endif
  514. }
  515. static void hss_hdlc_set_carrier(void *pdev, int carrier)
  516. {
  517. struct net_device *netdev = pdev;
  518. struct port *port = dev_to_port(netdev);
  519. unsigned long flags;
  520. spin_lock_irqsave(&npe_lock, flags);
  521. port->carrier = carrier;
  522. if (!port->loopback) {
  523. if (carrier)
  524. netif_carrier_on(netdev);
  525. else
  526. netif_carrier_off(netdev);
  527. }
  528. spin_unlock_irqrestore(&npe_lock, flags);
  529. }
  530. static void hss_hdlc_rx_irq(void *pdev)
  531. {
  532. struct net_device *dev = pdev;
  533. struct port *port = dev_to_port(dev);
  534. #if DEBUG_RX
  535. printk(KERN_DEBUG "%s: hss_hdlc_rx_irq\n", dev->name);
  536. #endif
  537. qmgr_disable_irq(queue_ids[port->id].rx);
  538. napi_schedule(&port->napi);
  539. }
  540. static int hss_hdlc_poll(struct napi_struct *napi, int budget)
  541. {
  542. struct port *port = container_of(napi, struct port, napi);
  543. struct net_device *dev = port->netdev;
  544. unsigned int rxq = queue_ids[port->id].rx;
  545. unsigned int rxfreeq = queue_ids[port->id].rxfree;
  546. int received = 0;
  547. #if DEBUG_RX
  548. printk(KERN_DEBUG "%s: hss_hdlc_poll\n", dev->name);
  549. #endif
  550. while (received < budget) {
  551. struct sk_buff *skb;
  552. struct desc *desc;
  553. int n;
  554. #ifdef __ARMEB__
  555. struct sk_buff *temp;
  556. u32 phys;
  557. #endif
  558. if ((n = queue_get_desc(rxq, port, 0)) < 0) {
  559. #if DEBUG_RX
  560. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  561. " napi_complete\n", dev->name);
  562. #endif
  563. napi_complete(napi);
  564. qmgr_enable_irq(rxq);
  565. if (!qmgr_stat_empty(rxq) &&
  566. napi_reschedule(napi)) {
  567. #if DEBUG_RX
  568. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  569. " napi_reschedule succeeded\n",
  570. dev->name);
  571. #endif
  572. qmgr_disable_irq(rxq);
  573. continue;
  574. }
  575. #if DEBUG_RX
  576. printk(KERN_DEBUG "%s: hss_hdlc_poll all done\n",
  577. dev->name);
  578. #endif
  579. return received; /* all work done */
  580. }
  581. desc = rx_desc_ptr(port, n);
  582. #if 0 /* FIXME - error_count counts modulo 256, perhaps we should use it */
  583. if (desc->error_count)
  584. printk(KERN_DEBUG "%s: hss_hdlc_poll status 0x%02X"
  585. " errors %u\n", dev->name, desc->status,
  586. desc->error_count);
  587. #endif
  588. skb = NULL;
  589. switch (desc->status) {
  590. case 0:
  591. #ifdef __ARMEB__
  592. if ((skb = netdev_alloc_skb(dev, RX_SIZE)) != NULL) {
  593. phys = dma_map_single(&dev->dev, skb->data,
  594. RX_SIZE,
  595. DMA_FROM_DEVICE);
  596. if (dma_mapping_error(&dev->dev, phys)) {
  597. dev_kfree_skb(skb);
  598. skb = NULL;
  599. }
  600. }
  601. #else
  602. skb = netdev_alloc_skb(dev, desc->pkt_len);
  603. #endif
  604. if (!skb)
  605. dev->stats.rx_dropped++;
  606. break;
  607. case ERR_HDLC_ALIGN:
  608. case ERR_HDLC_ABORT:
  609. dev->stats.rx_frame_errors++;
  610. dev->stats.rx_errors++;
  611. break;
  612. case ERR_HDLC_FCS:
  613. dev->stats.rx_crc_errors++;
  614. dev->stats.rx_errors++;
  615. break;
  616. case ERR_HDLC_TOO_LONG:
  617. dev->stats.rx_length_errors++;
  618. dev->stats.rx_errors++;
  619. break;
  620. default: /* FIXME - remove printk */
  621. netdev_err(dev, "hss_hdlc_poll: status 0x%02X errors %u\n",
  622. desc->status, desc->error_count);
  623. dev->stats.rx_errors++;
  624. }
  625. if (!skb) {
  626. /* put the desc back on RX-ready queue */
  627. desc->buf_len = RX_SIZE;
  628. desc->pkt_len = desc->status = 0;
  629. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  630. continue;
  631. }
  632. /* process received frame */
  633. #ifdef __ARMEB__
  634. temp = skb;
  635. skb = port->rx_buff_tab[n];
  636. dma_unmap_single(&dev->dev, desc->data,
  637. RX_SIZE, DMA_FROM_DEVICE);
  638. #else
  639. dma_sync_single_for_cpu(&dev->dev, desc->data,
  640. RX_SIZE, DMA_FROM_DEVICE);
  641. memcpy_swab32((u32 *)skb->data, (u32 *)port->rx_buff_tab[n],
  642. ALIGN(desc->pkt_len, 4) / 4);
  643. #endif
  644. skb_put(skb, desc->pkt_len);
  645. debug_pkt(dev, "hss_hdlc_poll", skb->data, skb->len);
  646. skb->protocol = hdlc_type_trans(skb, dev);
  647. dev->stats.rx_packets++;
  648. dev->stats.rx_bytes += skb->len;
  649. netif_receive_skb(skb);
  650. /* put the new buffer on RX-free queue */
  651. #ifdef __ARMEB__
  652. port->rx_buff_tab[n] = temp;
  653. desc->data = phys;
  654. #endif
  655. desc->buf_len = RX_SIZE;
  656. desc->pkt_len = 0;
  657. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  658. received++;
  659. }
  660. #if DEBUG_RX
  661. printk(KERN_DEBUG "hss_hdlc_poll: end, not all work done\n");
  662. #endif
  663. return received; /* not all work done */
  664. }
  665. static void hss_hdlc_txdone_irq(void *pdev)
  666. {
  667. struct net_device *dev = pdev;
  668. struct port *port = dev_to_port(dev);
  669. int n_desc;
  670. #if DEBUG_TX
  671. printk(KERN_DEBUG DRV_NAME ": hss_hdlc_txdone_irq\n");
  672. #endif
  673. while ((n_desc = queue_get_desc(queue_ids[port->id].txdone,
  674. port, 1)) >= 0) {
  675. struct desc *desc;
  676. int start;
  677. desc = tx_desc_ptr(port, n_desc);
  678. dev->stats.tx_packets++;
  679. dev->stats.tx_bytes += desc->pkt_len;
  680. dma_unmap_tx(port, desc);
  681. #if DEBUG_TX
  682. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq free %p\n",
  683. dev->name, port->tx_buff_tab[n_desc]);
  684. #endif
  685. free_buffer_irq(port->tx_buff_tab[n_desc]);
  686. port->tx_buff_tab[n_desc] = NULL;
  687. start = qmgr_stat_below_low_watermark(port->plat->txreadyq);
  688. queue_put_desc(port->plat->txreadyq,
  689. tx_desc_phys(port, n_desc), desc);
  690. if (start) { /* TX-ready queue was empty */
  691. #if DEBUG_TX
  692. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq xmit"
  693. " ready\n", dev->name);
  694. #endif
  695. netif_wake_queue(dev);
  696. }
  697. }
  698. }
  699. static int hss_hdlc_xmit(struct sk_buff *skb, struct net_device *dev)
  700. {
  701. struct port *port = dev_to_port(dev);
  702. unsigned int txreadyq = port->plat->txreadyq;
  703. int len, offset, bytes, n;
  704. void *mem;
  705. u32 phys;
  706. struct desc *desc;
  707. #if DEBUG_TX
  708. printk(KERN_DEBUG "%s: hss_hdlc_xmit\n", dev->name);
  709. #endif
  710. if (unlikely(skb->len > HDLC_MAX_MRU)) {
  711. dev_kfree_skb(skb);
  712. dev->stats.tx_errors++;
  713. return NETDEV_TX_OK;
  714. }
  715. debug_pkt(dev, "hss_hdlc_xmit", skb->data, skb->len);
  716. len = skb->len;
  717. #ifdef __ARMEB__
  718. offset = 0; /* no need to keep alignment */
  719. bytes = len;
  720. mem = skb->data;
  721. #else
  722. offset = (int)skb->data & 3; /* keep 32-bit alignment */
  723. bytes = ALIGN(offset + len, 4);
  724. if (!(mem = kmalloc(bytes, GFP_ATOMIC))) {
  725. dev_kfree_skb(skb);
  726. dev->stats.tx_dropped++;
  727. return NETDEV_TX_OK;
  728. }
  729. memcpy_swab32(mem, (u32 *)((int)skb->data & ~3), bytes / 4);
  730. dev_kfree_skb(skb);
  731. #endif
  732. phys = dma_map_single(&dev->dev, mem, bytes, DMA_TO_DEVICE);
  733. if (dma_mapping_error(&dev->dev, phys)) {
  734. #ifdef __ARMEB__
  735. dev_kfree_skb(skb);
  736. #else
  737. kfree(mem);
  738. #endif
  739. dev->stats.tx_dropped++;
  740. return NETDEV_TX_OK;
  741. }
  742. n = queue_get_desc(txreadyq, port, 1);
  743. BUG_ON(n < 0);
  744. desc = tx_desc_ptr(port, n);
  745. #ifdef __ARMEB__
  746. port->tx_buff_tab[n] = skb;
  747. #else
  748. port->tx_buff_tab[n] = mem;
  749. #endif
  750. desc->data = phys + offset;
  751. desc->buf_len = desc->pkt_len = len;
  752. wmb();
  753. queue_put_desc(queue_ids[port->id].tx, tx_desc_phys(port, n), desc);
  754. if (qmgr_stat_below_low_watermark(txreadyq)) { /* empty */
  755. #if DEBUG_TX
  756. printk(KERN_DEBUG "%s: hss_hdlc_xmit queue full\n", dev->name);
  757. #endif
  758. netif_stop_queue(dev);
  759. /* we could miss TX ready interrupt */
  760. if (!qmgr_stat_below_low_watermark(txreadyq)) {
  761. #if DEBUG_TX
  762. printk(KERN_DEBUG "%s: hss_hdlc_xmit ready again\n",
  763. dev->name);
  764. #endif
  765. netif_wake_queue(dev);
  766. }
  767. }
  768. #if DEBUG_TX
  769. printk(KERN_DEBUG "%s: hss_hdlc_xmit end\n", dev->name);
  770. #endif
  771. return NETDEV_TX_OK;
  772. }
  773. static int request_hdlc_queues(struct port *port)
  774. {
  775. int err;
  776. err = qmgr_request_queue(queue_ids[port->id].rxfree, RX_DESCS, 0, 0,
  777. "%s:RX-free", port->netdev->name);
  778. if (err)
  779. return err;
  780. err = qmgr_request_queue(queue_ids[port->id].rx, RX_DESCS, 0, 0,
  781. "%s:RX", port->netdev->name);
  782. if (err)
  783. goto rel_rxfree;
  784. err = qmgr_request_queue(queue_ids[port->id].tx, TX_DESCS, 0, 0,
  785. "%s:TX", port->netdev->name);
  786. if (err)
  787. goto rel_rx;
  788. err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
  789. "%s:TX-ready", port->netdev->name);
  790. if (err)
  791. goto rel_tx;
  792. err = qmgr_request_queue(queue_ids[port->id].txdone, TX_DESCS, 0, 0,
  793. "%s:TX-done", port->netdev->name);
  794. if (err)
  795. goto rel_txready;
  796. return 0;
  797. rel_txready:
  798. qmgr_release_queue(port->plat->txreadyq);
  799. rel_tx:
  800. qmgr_release_queue(queue_ids[port->id].tx);
  801. rel_rx:
  802. qmgr_release_queue(queue_ids[port->id].rx);
  803. rel_rxfree:
  804. qmgr_release_queue(queue_ids[port->id].rxfree);
  805. printk(KERN_DEBUG "%s: unable to request hardware queues\n",
  806. port->netdev->name);
  807. return err;
  808. }
  809. static void release_hdlc_queues(struct port *port)
  810. {
  811. qmgr_release_queue(queue_ids[port->id].rxfree);
  812. qmgr_release_queue(queue_ids[port->id].rx);
  813. qmgr_release_queue(queue_ids[port->id].txdone);
  814. qmgr_release_queue(queue_ids[port->id].tx);
  815. qmgr_release_queue(port->plat->txreadyq);
  816. }
  817. static int init_hdlc_queues(struct port *port)
  818. {
  819. int i;
  820. if (!ports_open)
  821. if (!(dma_pool = dma_pool_create(DRV_NAME, NULL,
  822. POOL_ALLOC_SIZE, 32, 0)))
  823. return -ENOMEM;
  824. if (!(port->desc_tab = dma_pool_alloc(dma_pool, GFP_KERNEL,
  825. &port->desc_tab_phys)))
  826. return -ENOMEM;
  827. memset(port->desc_tab, 0, POOL_ALLOC_SIZE);
  828. memset(port->rx_buff_tab, 0, sizeof(port->rx_buff_tab)); /* tables */
  829. memset(port->tx_buff_tab, 0, sizeof(port->tx_buff_tab));
  830. /* Setup RX buffers */
  831. for (i = 0; i < RX_DESCS; i++) {
  832. struct desc *desc = rx_desc_ptr(port, i);
  833. buffer_t *buff;
  834. void *data;
  835. #ifdef __ARMEB__
  836. if (!(buff = netdev_alloc_skb(port->netdev, RX_SIZE)))
  837. return -ENOMEM;
  838. data = buff->data;
  839. #else
  840. if (!(buff = kmalloc(RX_SIZE, GFP_KERNEL)))
  841. return -ENOMEM;
  842. data = buff;
  843. #endif
  844. desc->buf_len = RX_SIZE;
  845. desc->data = dma_map_single(&port->netdev->dev, data,
  846. RX_SIZE, DMA_FROM_DEVICE);
  847. if (dma_mapping_error(&port->netdev->dev, desc->data)) {
  848. free_buffer(buff);
  849. return -EIO;
  850. }
  851. port->rx_buff_tab[i] = buff;
  852. }
  853. return 0;
  854. }
  855. static void destroy_hdlc_queues(struct port *port)
  856. {
  857. int i;
  858. if (port->desc_tab) {
  859. for (i = 0; i < RX_DESCS; i++) {
  860. struct desc *desc = rx_desc_ptr(port, i);
  861. buffer_t *buff = port->rx_buff_tab[i];
  862. if (buff) {
  863. dma_unmap_single(&port->netdev->dev,
  864. desc->data, RX_SIZE,
  865. DMA_FROM_DEVICE);
  866. free_buffer(buff);
  867. }
  868. }
  869. for (i = 0; i < TX_DESCS; i++) {
  870. struct desc *desc = tx_desc_ptr(port, i);
  871. buffer_t *buff = port->tx_buff_tab[i];
  872. if (buff) {
  873. dma_unmap_tx(port, desc);
  874. free_buffer(buff);
  875. }
  876. }
  877. dma_pool_free(dma_pool, port->desc_tab, port->desc_tab_phys);
  878. port->desc_tab = NULL;
  879. }
  880. if (!ports_open && dma_pool) {
  881. dma_pool_destroy(dma_pool);
  882. dma_pool = NULL;
  883. }
  884. }
  885. static int hss_hdlc_open(struct net_device *dev)
  886. {
  887. struct port *port = dev_to_port(dev);
  888. unsigned long flags;
  889. int i, err = 0;
  890. if ((err = hdlc_open(dev)))
  891. return err;
  892. if ((err = hss_load_firmware(port)))
  893. goto err_hdlc_close;
  894. if ((err = request_hdlc_queues(port)))
  895. goto err_hdlc_close;
  896. if ((err = init_hdlc_queues(port)))
  897. goto err_destroy_queues;
  898. spin_lock_irqsave(&npe_lock, flags);
  899. if (port->plat->open)
  900. if ((err = port->plat->open(port->id, dev,
  901. hss_hdlc_set_carrier)))
  902. goto err_unlock;
  903. spin_unlock_irqrestore(&npe_lock, flags);
  904. /* Populate queues with buffers, no failure after this point */
  905. for (i = 0; i < TX_DESCS; i++)
  906. queue_put_desc(port->plat->txreadyq,
  907. tx_desc_phys(port, i), tx_desc_ptr(port, i));
  908. for (i = 0; i < RX_DESCS; i++)
  909. queue_put_desc(queue_ids[port->id].rxfree,
  910. rx_desc_phys(port, i), rx_desc_ptr(port, i));
  911. napi_enable(&port->napi);
  912. netif_start_queue(dev);
  913. qmgr_set_irq(queue_ids[port->id].rx, QUEUE_IRQ_SRC_NOT_EMPTY,
  914. hss_hdlc_rx_irq, dev);
  915. qmgr_set_irq(queue_ids[port->id].txdone, QUEUE_IRQ_SRC_NOT_EMPTY,
  916. hss_hdlc_txdone_irq, dev);
  917. qmgr_enable_irq(queue_ids[port->id].txdone);
  918. ports_open++;
  919. hss_set_hdlc_cfg(port);
  920. hss_config(port);
  921. hss_start_hdlc(port);
  922. /* we may already have RX data, enables IRQ */
  923. napi_schedule(&port->napi);
  924. return 0;
  925. err_unlock:
  926. spin_unlock_irqrestore(&npe_lock, flags);
  927. err_destroy_queues:
  928. destroy_hdlc_queues(port);
  929. release_hdlc_queues(port);
  930. err_hdlc_close:
  931. hdlc_close(dev);
  932. return err;
  933. }
  934. static int hss_hdlc_close(struct net_device *dev)
  935. {
  936. struct port *port = dev_to_port(dev);
  937. unsigned long flags;
  938. int i, buffs = RX_DESCS; /* allocated RX buffers */
  939. spin_lock_irqsave(&npe_lock, flags);
  940. ports_open--;
  941. qmgr_disable_irq(queue_ids[port->id].rx);
  942. netif_stop_queue(dev);
  943. napi_disable(&port->napi);
  944. hss_stop_hdlc(port);
  945. while (queue_get_desc(queue_ids[port->id].rxfree, port, 0) >= 0)
  946. buffs--;
  947. while (queue_get_desc(queue_ids[port->id].rx, port, 0) >= 0)
  948. buffs--;
  949. if (buffs)
  950. netdev_crit(dev, "unable to drain RX queue, %i buffer(s) left in NPE\n",
  951. buffs);
  952. buffs = TX_DESCS;
  953. while (queue_get_desc(queue_ids[port->id].tx, port, 1) >= 0)
  954. buffs--; /* cancel TX */
  955. i = 0;
  956. do {
  957. while (queue_get_desc(port->plat->txreadyq, port, 1) >= 0)
  958. buffs--;
  959. if (!buffs)
  960. break;
  961. } while (++i < MAX_CLOSE_WAIT);
  962. if (buffs)
  963. netdev_crit(dev, "unable to drain TX queue, %i buffer(s) left in NPE\n",
  964. buffs);
  965. #if DEBUG_CLOSE
  966. if (!buffs)
  967. printk(KERN_DEBUG "Draining TX queues took %i cycles\n", i);
  968. #endif
  969. qmgr_disable_irq(queue_ids[port->id].txdone);
  970. if (port->plat->close)
  971. port->plat->close(port->id, dev);
  972. spin_unlock_irqrestore(&npe_lock, flags);
  973. destroy_hdlc_queues(port);
  974. release_hdlc_queues(port);
  975. hdlc_close(dev);
  976. return 0;
  977. }
  978. static int hss_hdlc_attach(struct net_device *dev, unsigned short encoding,
  979. unsigned short parity)
  980. {
  981. struct port *port = dev_to_port(dev);
  982. if (encoding != ENCODING_NRZ)
  983. return -EINVAL;
  984. switch(parity) {
  985. case PARITY_CRC16_PR1_CCITT:
  986. port->hdlc_cfg = 0;
  987. return 0;
  988. case PARITY_CRC32_PR1_CCITT:
  989. port->hdlc_cfg = PKT_HDLC_CRC_32;
  990. return 0;
  991. default:
  992. return -EINVAL;
  993. }
  994. }
  995. static u32 check_clock(u32 rate, u32 a, u32 b, u32 c,
  996. u32 *best, u32 *best_diff, u32 *reg)
  997. {
  998. /* a is 10-bit, b is 10-bit, c is 12-bit */
  999. u64 new_rate;
  1000. u32 new_diff;
  1001. new_rate = ixp4xx_timer_freq * (u64)(c + 1);
  1002. do_div(new_rate, a * (c + 1) + b + 1);
  1003. new_diff = abs((u32)new_rate - rate);
  1004. if (new_diff < *best_diff) {
  1005. *best = new_rate;
  1006. *best_diff = new_diff;
  1007. *reg = (a << 22) | (b << 12) | c;
  1008. }
  1009. return new_diff;
  1010. }
  1011. static void find_best_clock(u32 rate, u32 *best, u32 *reg)
  1012. {
  1013. u32 a, b, diff = 0xFFFFFFFF;
  1014. a = ixp4xx_timer_freq / rate;
  1015. if (a > 0x3FF) { /* 10-bit value - we can go as slow as ca. 65 kb/s */
  1016. check_clock(rate, 0x3FF, 1, 1, best, &diff, reg);
  1017. return;
  1018. }
  1019. if (a == 0) { /* > 66.666 MHz */
  1020. a = 1; /* minimum divider is 1 (a = 0, b = 1, c = 1) */
  1021. rate = ixp4xx_timer_freq;
  1022. }
  1023. if (rate * a == ixp4xx_timer_freq) { /* don't divide by 0 later */
  1024. check_clock(rate, a - 1, 1, 1, best, &diff, reg);
  1025. return;
  1026. }
  1027. for (b = 0; b < 0x400; b++) {
  1028. u64 c = (b + 1) * (u64)rate;
  1029. do_div(c, ixp4xx_timer_freq - rate * a);
  1030. c--;
  1031. if (c >= 0xFFF) { /* 12-bit - no need to check more 'b's */
  1032. if (b == 0 && /* also try a bit higher rate */
  1033. !check_clock(rate, a - 1, 1, 1, best, &diff, reg))
  1034. return;
  1035. check_clock(rate, a, b, 0xFFF, best, &diff, reg);
  1036. return;
  1037. }
  1038. if (!check_clock(rate, a, b, c, best, &diff, reg))
  1039. return;
  1040. if (!check_clock(rate, a, b, c + 1, best, &diff, reg))
  1041. return;
  1042. }
  1043. }
  1044. static int hss_hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1045. {
  1046. const size_t size = sizeof(sync_serial_settings);
  1047. sync_serial_settings new_line;
  1048. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1049. struct port *port = dev_to_port(dev);
  1050. unsigned long flags;
  1051. int clk;
  1052. if (cmd != SIOCWANDEV)
  1053. return hdlc_ioctl(dev, ifr, cmd);
  1054. switch(ifr->ifr_settings.type) {
  1055. case IF_GET_IFACE:
  1056. ifr->ifr_settings.type = IF_IFACE_V35;
  1057. if (ifr->ifr_settings.size < size) {
  1058. ifr->ifr_settings.size = size; /* data size wanted */
  1059. return -ENOBUFS;
  1060. }
  1061. memset(&new_line, 0, sizeof(new_line));
  1062. new_line.clock_type = port->clock_type;
  1063. new_line.clock_rate = port->clock_rate;
  1064. new_line.loopback = port->loopback;
  1065. if (copy_to_user(line, &new_line, size))
  1066. return -EFAULT;
  1067. return 0;
  1068. case IF_IFACE_SYNC_SERIAL:
  1069. case IF_IFACE_V35:
  1070. if(!capable(CAP_NET_ADMIN))
  1071. return -EPERM;
  1072. if (copy_from_user(&new_line, line, size))
  1073. return -EFAULT;
  1074. clk = new_line.clock_type;
  1075. if (port->plat->set_clock)
  1076. clk = port->plat->set_clock(port->id, clk);
  1077. if (clk != CLOCK_EXT && clk != CLOCK_INT)
  1078. return -EINVAL; /* No such clock setting */
  1079. if (new_line.loopback != 0 && new_line.loopback != 1)
  1080. return -EINVAL;
  1081. port->clock_type = clk; /* Update settings */
  1082. if (clk == CLOCK_INT)
  1083. find_best_clock(new_line.clock_rate, &port->clock_rate,
  1084. &port->clock_reg);
  1085. else {
  1086. port->clock_rate = 0;
  1087. port->clock_reg = CLK42X_SPEED_2048KHZ;
  1088. }
  1089. port->loopback = new_line.loopback;
  1090. spin_lock_irqsave(&npe_lock, flags);
  1091. if (dev->flags & IFF_UP)
  1092. hss_config(port);
  1093. if (port->loopback || port->carrier)
  1094. netif_carrier_on(port->netdev);
  1095. else
  1096. netif_carrier_off(port->netdev);
  1097. spin_unlock_irqrestore(&npe_lock, flags);
  1098. return 0;
  1099. default:
  1100. return hdlc_ioctl(dev, ifr, cmd);
  1101. }
  1102. }
  1103. /*****************************************************************************
  1104. * initialization
  1105. ****************************************************************************/
  1106. static const struct net_device_ops hss_hdlc_ops = {
  1107. .ndo_open = hss_hdlc_open,
  1108. .ndo_stop = hss_hdlc_close,
  1109. .ndo_change_mtu = hdlc_change_mtu,
  1110. .ndo_start_xmit = hdlc_start_xmit,
  1111. .ndo_do_ioctl = hss_hdlc_ioctl,
  1112. };
  1113. static int __devinit hss_init_one(struct platform_device *pdev)
  1114. {
  1115. struct port *port;
  1116. struct net_device *dev;
  1117. hdlc_device *hdlc;
  1118. int err;
  1119. if ((port = kzalloc(sizeof(*port), GFP_KERNEL)) == NULL)
  1120. return -ENOMEM;
  1121. if ((port->npe = npe_request(0)) == NULL) {
  1122. err = -ENODEV;
  1123. goto err_free;
  1124. }
  1125. if ((port->netdev = dev = alloc_hdlcdev(port)) == NULL) {
  1126. err = -ENOMEM;
  1127. goto err_plat;
  1128. }
  1129. SET_NETDEV_DEV(dev, &pdev->dev);
  1130. hdlc = dev_to_hdlc(dev);
  1131. hdlc->attach = hss_hdlc_attach;
  1132. hdlc->xmit = hss_hdlc_xmit;
  1133. dev->netdev_ops = &hss_hdlc_ops;
  1134. dev->tx_queue_len = 100;
  1135. port->clock_type = CLOCK_EXT;
  1136. port->clock_rate = 0;
  1137. port->clock_reg = CLK42X_SPEED_2048KHZ;
  1138. port->id = pdev->id;
  1139. port->dev = &pdev->dev;
  1140. port->plat = pdev->dev.platform_data;
  1141. netif_napi_add(dev, &port->napi, hss_hdlc_poll, NAPI_WEIGHT);
  1142. if ((err = register_hdlc_device(dev)))
  1143. goto err_free_netdev;
  1144. platform_set_drvdata(pdev, port);
  1145. netdev_info(dev, "HSS-%i\n", port->id);
  1146. return 0;
  1147. err_free_netdev:
  1148. free_netdev(dev);
  1149. err_plat:
  1150. npe_release(port->npe);
  1151. err_free:
  1152. kfree(port);
  1153. return err;
  1154. }
  1155. static int __devexit hss_remove_one(struct platform_device *pdev)
  1156. {
  1157. struct port *port = platform_get_drvdata(pdev);
  1158. unregister_hdlc_device(port->netdev);
  1159. free_netdev(port->netdev);
  1160. npe_release(port->npe);
  1161. platform_set_drvdata(pdev, NULL);
  1162. kfree(port);
  1163. return 0;
  1164. }
  1165. static struct platform_driver ixp4xx_hss_driver = {
  1166. .driver.name = DRV_NAME,
  1167. .probe = hss_init_one,
  1168. .remove = hss_remove_one,
  1169. };
  1170. static int __init hss_init_module(void)
  1171. {
  1172. if ((ixp4xx_read_feature_bits() &
  1173. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS)) !=
  1174. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS))
  1175. return -ENODEV;
  1176. spin_lock_init(&npe_lock);
  1177. return platform_driver_register(&ixp4xx_hss_driver);
  1178. }
  1179. static void __exit hss_cleanup_module(void)
  1180. {
  1181. platform_driver_unregister(&ixp4xx_hss_driver);
  1182. }
  1183. MODULE_AUTHOR("Krzysztof Halasa");
  1184. MODULE_DESCRIPTION("Intel IXP4xx HSS driver");
  1185. MODULE_LICENSE("GPL v2");
  1186. MODULE_ALIAS("platform:ixp4xx_hss");
  1187. module_init(hss_init_module);
  1188. module_exit(hss_cleanup_module);