11da177e4SLinus Torvalds /* 2d23eee88SFinn Thain * Device driver for the Cuda and Egret system controllers found on PowerMacs 3d23eee88SFinn Thain * and 68k Macs. 41da177e4SLinus Torvalds * 5d23eee88SFinn Thain * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA. 6d23eee88SFinn Thain * This MCU controls system power, Parameter RAM, Real Time Clock and the 7d23eee88SFinn Thain * Apple Desktop Bus (ADB) that connects to the keyboard and mouse. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Copyright (C) 1996 Paul Mackerras. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds #include <stdarg.h> 121da177e4SLinus Torvalds #include <linux/types.h> 131da177e4SLinus Torvalds #include <linux/errno.h> 141da177e4SLinus Torvalds #include <linux/kernel.h> 151da177e4SLinus Torvalds #include <linux/delay.h> 161da177e4SLinus Torvalds #include <linux/adb.h> 171da177e4SLinus Torvalds #include <linux/cuda.h> 181da177e4SLinus Torvalds #include <linux/spinlock.h> 191da177e4SLinus Torvalds #include <linux/interrupt.h> 201da177e4SLinus Torvalds #ifdef CONFIG_PPC 211da177e4SLinus Torvalds #include <asm/prom.h> 221da177e4SLinus Torvalds #include <asm/machdep.h> 231da177e4SLinus Torvalds #else 241da177e4SLinus Torvalds #include <asm/macintosh.h> 251da177e4SLinus Torvalds #include <asm/macints.h> 261da177e4SLinus Torvalds #include <asm/mac_via.h> 271da177e4SLinus Torvalds #endif 281da177e4SLinus Torvalds #include <asm/io.h> 291da177e4SLinus Torvalds #include <linux/init.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds static volatile unsigned char __iomem *via; 321da177e4SLinus Torvalds static DEFINE_SPINLOCK(cuda_lock); 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds /* VIA registers - spaced 0x200 bytes apart */ 351da177e4SLinus Torvalds #define RS 0x200 /* skip between registers */ 361da177e4SLinus Torvalds #define B 0 /* B-side data */ 371da177e4SLinus Torvalds #define A RS /* A-side data */ 381da177e4SLinus Torvalds #define DIRB (2*RS) /* B-side direction (1=output) */ 391da177e4SLinus Torvalds #define DIRA (3*RS) /* A-side direction (1=output) */ 401da177e4SLinus Torvalds #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */ 411da177e4SLinus Torvalds #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */ 421da177e4SLinus Torvalds #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */ 431da177e4SLinus Torvalds #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */ 441da177e4SLinus Torvalds #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */ 451da177e4SLinus Torvalds #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */ 461da177e4SLinus Torvalds #define SR (10*RS) /* Shift register */ 471da177e4SLinus Torvalds #define ACR (11*RS) /* Auxiliary control register */ 481da177e4SLinus Torvalds #define PCR (12*RS) /* Peripheral control register */ 491da177e4SLinus Torvalds #define IFR (13*RS) /* Interrupt flag register */ 501da177e4SLinus Torvalds #define IER (14*RS) /* Interrupt enable register */ 511da177e4SLinus Torvalds #define ANH (15*RS) /* A-side data, no handshake */ 521da177e4SLinus Torvalds 53d23eee88SFinn Thain /* 54d23eee88SFinn Thain * When the Cuda design replaced the Egret, some signal names and 55d23eee88SFinn Thain * logic sense changed. They all serve the same purposes, however. 56d23eee88SFinn Thain * 57d23eee88SFinn Thain * VIA pin | Egret pin 58d23eee88SFinn Thain * ----------------+------------------------------------------ 59d23eee88SFinn Thain * PB3 (input) | Transceiver session (active low) 60d23eee88SFinn Thain * PB4 (output) | VIA full (active high) 61d23eee88SFinn Thain * PB5 (output) | System session (active high) 62d23eee88SFinn Thain * 63d23eee88SFinn Thain * VIA pin | Cuda pin 64d23eee88SFinn Thain * ----------------+------------------------------------------ 65d23eee88SFinn Thain * PB3 (input) | Transfer request (active low) 66d23eee88SFinn Thain * PB4 (output) | Byte acknowledge (active low) 67d23eee88SFinn Thain * PB5 (output) | Transfer in progress (active low) 68d23eee88SFinn Thain */ 69d23eee88SFinn Thain 70d23eee88SFinn Thain /* Bits in Port B data register */ 71d23eee88SFinn Thain #define TREQ 0x08 /* Transfer request */ 72d23eee88SFinn Thain #define TACK 0x10 /* Transfer acknowledge */ 73d23eee88SFinn Thain #define TIP 0x20 /* Transfer in progress */ 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* Bits in ACR */ 761da177e4SLinus Torvalds #define SR_CTRL 0x1c /* Shift register control bits */ 771da177e4SLinus Torvalds #define SR_EXT 0x0c /* Shift on external clock */ 781da177e4SLinus Torvalds #define SR_OUT 0x10 /* Shift out if 1 */ 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /* Bits in IFR and IER */ 811da177e4SLinus Torvalds #define IER_SET 0x80 /* set bits in IER */ 821da177e4SLinus Torvalds #define IER_CLR 0 /* clear bits in IER */ 831da177e4SLinus Torvalds #define SR_INT 0x04 /* Shift register full/empty */ 841da177e4SLinus Torvalds 85d23eee88SFinn Thain /* Duration of byte acknowledgement pulse (us) */ 86d23eee88SFinn Thain #define EGRET_TACK_ASSERTED_DELAY 300 87d23eee88SFinn Thain #define EGRET_TACK_NEGATED_DELAY 400 88d23eee88SFinn Thain 89d23eee88SFinn Thain /* Interval from interrupt to start of session (us) */ 90d23eee88SFinn Thain #define EGRET_SESSION_DELAY 450 91d23eee88SFinn Thain 92d23eee88SFinn Thain #ifdef CONFIG_PPC 93d23eee88SFinn Thain #define mcu_is_egret false 94d23eee88SFinn Thain #else 95d23eee88SFinn Thain static bool mcu_is_egret; 96d23eee88SFinn Thain #endif 97d23eee88SFinn Thain 98fd7a65a2SFinn Thain static inline bool TREQ_asserted(u8 portb) 99fd7a65a2SFinn Thain { 100fd7a65a2SFinn Thain return !(portb & TREQ); 101fd7a65a2SFinn Thain } 102fd7a65a2SFinn Thain 103fd7a65a2SFinn Thain static inline void assert_TIP(void) 104fd7a65a2SFinn Thain { 105d23eee88SFinn Thain if (mcu_is_egret) { 106d23eee88SFinn Thain udelay(EGRET_SESSION_DELAY); 107d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) | TIP); 108d23eee88SFinn Thain } else 109fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) & ~TIP); 110fd7a65a2SFinn Thain } 111fd7a65a2SFinn Thain 112d23eee88SFinn Thain static inline void assert_TIP_and_TACK(void) 113d23eee88SFinn Thain { 114d23eee88SFinn Thain if (mcu_is_egret) { 115d23eee88SFinn Thain udelay(EGRET_SESSION_DELAY); 116d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) | TIP | TACK); 117d23eee88SFinn Thain } else 118d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK)); 119d23eee88SFinn Thain } 120d23eee88SFinn Thain 121fd7a65a2SFinn Thain static inline void assert_TACK(void) 122fd7a65a2SFinn Thain { 123d23eee88SFinn Thain if (mcu_is_egret) { 124d23eee88SFinn Thain udelay(EGRET_TACK_NEGATED_DELAY); 125d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) | TACK); 126d23eee88SFinn Thain } else 127fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) & ~TACK); 128fd7a65a2SFinn Thain } 129fd7a65a2SFinn Thain 130fd7a65a2SFinn Thain static inline void toggle_TACK(void) 131fd7a65a2SFinn Thain { 132fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) ^ TACK); 133fd7a65a2SFinn Thain } 134fd7a65a2SFinn Thain 135fd7a65a2SFinn Thain static inline void negate_TACK(void) 136fd7a65a2SFinn Thain { 137d23eee88SFinn Thain if (mcu_is_egret) { 138d23eee88SFinn Thain udelay(EGRET_TACK_ASSERTED_DELAY); 139d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) & ~TACK); 140d23eee88SFinn Thain } else 141fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) | TACK); 142fd7a65a2SFinn Thain } 143fd7a65a2SFinn Thain 144fd7a65a2SFinn Thain static inline void negate_TIP_and_TACK(void) 145fd7a65a2SFinn Thain { 146d23eee88SFinn Thain if (mcu_is_egret) { 147d23eee88SFinn Thain udelay(EGRET_TACK_ASSERTED_DELAY); 148d23eee88SFinn Thain out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK)); 149d23eee88SFinn Thain } else 150fd7a65a2SFinn Thain out_8(&via[B], in_8(&via[B]) | TIP | TACK); 151fd7a65a2SFinn Thain } 152fd7a65a2SFinn Thain 1531da177e4SLinus Torvalds static enum cuda_state { 1541da177e4SLinus Torvalds idle, 1551da177e4SLinus Torvalds sent_first_byte, 1561da177e4SLinus Torvalds sending, 1571da177e4SLinus Torvalds reading, 1581da177e4SLinus Torvalds read_done, 1591da177e4SLinus Torvalds awaiting_reply 1601da177e4SLinus Torvalds } cuda_state; 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds static struct adb_request *current_req; 1631da177e4SLinus Torvalds static struct adb_request *last_req; 1641da177e4SLinus Torvalds static unsigned char cuda_rbuf[16]; 1651da177e4SLinus Torvalds static unsigned char *reply_ptr; 1661da177e4SLinus Torvalds static int reading_reply; 1671da177e4SLinus Torvalds static int data_index; 1680251c38cSFinn Thain static int cuda_irq; 1691da177e4SLinus Torvalds #ifdef CONFIG_PPC 1701da177e4SLinus Torvalds static struct device_node *vias; 1711da177e4SLinus Torvalds #endif 17287275856SOlaf Hering static int cuda_fully_inited; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds #ifdef CONFIG_ADB 1751da177e4SLinus Torvalds static int cuda_probe(void); 1761da177e4SLinus Torvalds static int cuda_send_request(struct adb_request *req, int sync); 1771da177e4SLinus Torvalds static int cuda_adb_autopoll(int devs); 1781da177e4SLinus Torvalds static int cuda_reset_adb_bus(void); 1791da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static int cuda_init_via(void); 1821da177e4SLinus Torvalds static void cuda_start(void); 1837d12e780SDavid Howells static irqreturn_t cuda_interrupt(int irq, void *arg); 1847d12e780SDavid Howells static void cuda_input(unsigned char *buf, int nb); 1851da177e4SLinus Torvalds void cuda_poll(void); 1861da177e4SLinus Torvalds static int cuda_write(struct adb_request *req); 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds int cuda_request(struct adb_request *req, 1891da177e4SLinus Torvalds void (*done)(struct adb_request *), int nbytes, ...); 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds #ifdef CONFIG_ADB 1921da177e4SLinus Torvalds struct adb_driver via_cuda_driver = { 19318814ee8SFinn Thain .name = "CUDA", 19418814ee8SFinn Thain .probe = cuda_probe, 19518814ee8SFinn Thain .send_request = cuda_send_request, 19618814ee8SFinn Thain .autopoll = cuda_adb_autopoll, 19718814ee8SFinn Thain .poll = cuda_poll, 19818814ee8SFinn Thain .reset_bus = cuda_reset_adb_bus, 1991da177e4SLinus Torvalds }; 2001da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 2011da177e4SLinus Torvalds 20218814ee8SFinn Thain #ifdef CONFIG_MAC 20318814ee8SFinn Thain int __init find_via_cuda(void) 20418814ee8SFinn Thain { 20518814ee8SFinn Thain struct adb_request req; 20618814ee8SFinn Thain int err; 20718814ee8SFinn Thain 208*f74faec6SFinn Thain if (macintosh_config->adb_type != MAC_ADB_CUDA && 209*f74faec6SFinn Thain macintosh_config->adb_type != MAC_ADB_EGRET) 21018814ee8SFinn Thain return 0; 21118814ee8SFinn Thain 21218814ee8SFinn Thain via = via1; 21318814ee8SFinn Thain cuda_state = idle; 214*f74faec6SFinn Thain mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET; 21518814ee8SFinn Thain 21618814ee8SFinn Thain err = cuda_init_via(); 21718814ee8SFinn Thain if (err) { 21818814ee8SFinn Thain printk(KERN_ERR "cuda_init_via() failed\n"); 21918814ee8SFinn Thain via = NULL; 22018814ee8SFinn Thain return 0; 22118814ee8SFinn Thain } 22218814ee8SFinn Thain 22318814ee8SFinn Thain /* enable autopoll */ 22418814ee8SFinn Thain cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1); 22518814ee8SFinn Thain while (!req.complete) 22618814ee8SFinn Thain cuda_poll(); 22718814ee8SFinn Thain 22818814ee8SFinn Thain return 1; 22918814ee8SFinn Thain } 23018814ee8SFinn Thain #else 23151d3082fSBenjamin Herrenschmidt int __init find_via_cuda(void) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds struct adb_request req; 23451d3082fSBenjamin Herrenschmidt phys_addr_t taddr; 235018a3d1dSJeremy Kerr const u32 *reg; 23651d3082fSBenjamin Herrenschmidt int err; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds if (vias != 0) 2391da177e4SLinus Torvalds return 1; 24051d3082fSBenjamin Herrenschmidt vias = of_find_node_by_name(NULL, "via-cuda"); 2411da177e4SLinus Torvalds if (vias == 0) 2421da177e4SLinus Torvalds return 0; 2431da177e4SLinus Torvalds 24401b2726dSStephen Rothwell reg = of_get_property(vias, "reg", NULL); 24551d3082fSBenjamin Herrenschmidt if (reg == NULL) { 24651d3082fSBenjamin Herrenschmidt printk(KERN_ERR "via-cuda: No \"reg\" property !\n"); 24751d3082fSBenjamin Herrenschmidt goto fail; 2481da177e4SLinus Torvalds } 24951d3082fSBenjamin Herrenschmidt taddr = of_translate_address(vias, reg); 25051d3082fSBenjamin Herrenschmidt if (taddr == 0) { 25151d3082fSBenjamin Herrenschmidt printk(KERN_ERR "via-cuda: Can't translate address !\n"); 25251d3082fSBenjamin Herrenschmidt goto fail; 25351d3082fSBenjamin Herrenschmidt } 25451d3082fSBenjamin Herrenschmidt via = ioremap(taddr, 0x2000); 25551d3082fSBenjamin Herrenschmidt if (via == NULL) { 25651d3082fSBenjamin Herrenschmidt printk(KERN_ERR "via-cuda: Can't map address !\n"); 25751d3082fSBenjamin Herrenschmidt goto fail; 25851d3082fSBenjamin Herrenschmidt } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds cuda_state = idle; 2611da177e4SLinus Torvalds sys_ctrler = SYS_CTRLER_CUDA; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds err = cuda_init_via(); 2641da177e4SLinus Torvalds if (err) { 2651da177e4SLinus Torvalds printk(KERN_ERR "cuda_init_via() failed\n"); 2661da177e4SLinus Torvalds via = NULL; 2671da177e4SLinus Torvalds return 0; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds /* Clear and enable interrupts, but only on PPC. On 68K it's done */ 2711da177e4SLinus Torvalds /* for us by the main VIA driver in arch/m68k/mac/via.c */ 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */ 2741da177e4SLinus Torvalds out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */ 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* enable autopoll */ 2771da177e4SLinus Torvalds cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1); 2781da177e4SLinus Torvalds while (!req.complete) 2791da177e4SLinus Torvalds cuda_poll(); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds return 1; 28251d3082fSBenjamin Herrenschmidt 28351d3082fSBenjamin Herrenschmidt fail: 28451d3082fSBenjamin Herrenschmidt of_node_put(vias); 28551d3082fSBenjamin Herrenschmidt vias = NULL; 28651d3082fSBenjamin Herrenschmidt return 0; 2871da177e4SLinus Torvalds } 28818814ee8SFinn Thain #endif /* !defined CONFIG_MAC */ 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds static int __init via_cuda_start(void) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds if (via == NULL) 2931da177e4SLinus Torvalds return -ENODEV; 2941da177e4SLinus Torvalds 2950ebfff14SBenjamin Herrenschmidt #ifdef CONFIG_MAC 2960251c38cSFinn Thain cuda_irq = IRQ_MAC_ADB; 29718814ee8SFinn Thain #else 2980251c38cSFinn Thain cuda_irq = irq_of_parse_and_map(vias, 0); 299ef24ba70SMichael Ellerman if (!cuda_irq) { 3000ebfff14SBenjamin Herrenschmidt printk(KERN_ERR "via-cuda: can't map interrupts for %s\n", 3010ebfff14SBenjamin Herrenschmidt vias->full_name); 3020ebfff14SBenjamin Herrenschmidt return -ENODEV; 3030ebfff14SBenjamin Herrenschmidt } 30418814ee8SFinn Thain #endif 3050ebfff14SBenjamin Herrenschmidt 3060251c38cSFinn Thain if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) { 3070251c38cSFinn Thain printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq); 3081da177e4SLinus Torvalds return -EAGAIN; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 311d23eee88SFinn Thain pr_info("Macintosh Cuda and Egret driver.\n"); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds cuda_fully_inited = 1; 3141da177e4SLinus Torvalds return 0; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds device_initcall(via_cuda_start); 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds #ifdef CONFIG_ADB 3201da177e4SLinus Torvalds static int 3211da177e4SLinus Torvalds cuda_probe(void) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds #ifdef CONFIG_PPC 3241da177e4SLinus Torvalds if (sys_ctrler != SYS_CTRLER_CUDA) 3251da177e4SLinus Torvalds return -ENODEV; 3261da177e4SLinus Torvalds #else 327*f74faec6SFinn Thain if (macintosh_config->adb_type != MAC_ADB_CUDA && 328*f74faec6SFinn Thain macintosh_config->adb_type != MAC_ADB_EGRET) 3291da177e4SLinus Torvalds return -ENODEV; 3301da177e4SLinus Torvalds #endif 3311da177e4SLinus Torvalds if (via == NULL) 3321da177e4SLinus Torvalds return -ENODEV; 3331da177e4SLinus Torvalds return 0; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 3361da177e4SLinus Torvalds 337d23eee88SFinn Thain static int __init sync_egret(void) 338d23eee88SFinn Thain { 339d23eee88SFinn Thain if (TREQ_asserted(in_8(&via[B]))) { 340d23eee88SFinn Thain /* Complete the inbound transfer */ 341d23eee88SFinn Thain assert_TIP_and_TACK(); 342d23eee88SFinn Thain while (1) { 343d23eee88SFinn Thain negate_TACK(); 344d23eee88SFinn Thain mdelay(1); 345d23eee88SFinn Thain (void)in_8(&via[SR]); 346d23eee88SFinn Thain assert_TACK(); 347d23eee88SFinn Thain if (!TREQ_asserted(in_8(&via[B]))) 348d23eee88SFinn Thain break; 349d23eee88SFinn Thain } 350d23eee88SFinn Thain negate_TIP_and_TACK(); 351d23eee88SFinn Thain } else if (in_8(&via[B]) & TIP) { 352d23eee88SFinn Thain /* Terminate the outbound transfer */ 353d23eee88SFinn Thain negate_TACK(); 354d23eee88SFinn Thain assert_TACK(); 355d23eee88SFinn Thain mdelay(1); 356d23eee88SFinn Thain negate_TIP_and_TACK(); 357d23eee88SFinn Thain } 358d23eee88SFinn Thain /* Clear shift register interrupt */ 359d23eee88SFinn Thain if (in_8(&via[IFR]) & SR_INT) 360d23eee88SFinn Thain (void)in_8(&via[SR]); 361d23eee88SFinn Thain return 0; 362d23eee88SFinn Thain } 363d23eee88SFinn Thain 3641da177e4SLinus Torvalds #define WAIT_FOR(cond, what) \ 3651da177e4SLinus Torvalds do { \ 3661da177e4SLinus Torvalds int x; \ 3671da177e4SLinus Torvalds for (x = 1000; !(cond); --x) { \ 3681da177e4SLinus Torvalds if (x == 0) { \ 369523717d1SFinn Thain pr_err("Timeout waiting for " what "\n"); \ 3701da177e4SLinus Torvalds return -ENXIO; \ 3711da177e4SLinus Torvalds } \ 3721da177e4SLinus Torvalds udelay(100); \ 3731da177e4SLinus Torvalds } \ 3741da177e4SLinus Torvalds } while (0) 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds static int 377330dae19SGeert Uytterhoeven __init cuda_init_via(void) 3781da177e4SLinus Torvalds { 3790251c38cSFinn Thain #ifdef CONFIG_PPC 3801da177e4SLinus Torvalds out_8(&via[IER], 0x7f); /* disable interrupts from VIA */ 3811da177e4SLinus Torvalds (void)in_8(&via[IER]); 3820251c38cSFinn Thain #else 3830251c38cSFinn Thain out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */ 3841da177e4SLinus Torvalds #endif 3851da177e4SLinus Torvalds 386d23eee88SFinn Thain out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */ 387d23eee88SFinn Thain out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */ 388d23eee88SFinn Thain (void)in_8(&via[SR]); /* clear any left-over data */ 389d23eee88SFinn Thain 390d23eee88SFinn Thain if (mcu_is_egret) 391d23eee88SFinn Thain return sync_egret(); 392d23eee88SFinn Thain 393d23eee88SFinn Thain negate_TIP_and_TACK(); 394d23eee88SFinn Thain 3951da177e4SLinus Torvalds /* delay 4ms and then clear any pending interrupt */ 3961da177e4SLinus Torvalds mdelay(4); 3971da177e4SLinus Torvalds (void)in_8(&via[SR]); 3980251c38cSFinn Thain out_8(&via[IFR], SR_INT); 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* sync with the CUDA - assert TACK without TIP */ 401fd7a65a2SFinn Thain assert_TACK(); 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds /* wait for the CUDA to assert TREQ in response */ 404fd7a65a2SFinn Thain WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync"); 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* wait for the interrupt and then clear it */ 4071da177e4SLinus Torvalds WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)"); 4081da177e4SLinus Torvalds (void)in_8(&via[SR]); 4090251c38cSFinn Thain out_8(&via[IFR], SR_INT); 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /* finish the sync by negating TACK */ 412fd7a65a2SFinn Thain negate_TACK(); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds /* wait for the CUDA to negate TREQ and the corresponding interrupt */ 415fd7a65a2SFinn Thain WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)"); 4161da177e4SLinus Torvalds WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)"); 4171da177e4SLinus Torvalds (void)in_8(&via[SR]); 4180251c38cSFinn Thain out_8(&via[IFR], SR_INT); 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds return 0; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds #ifdef CONFIG_ADB 4241da177e4SLinus Torvalds /* Send an ADB command */ 4251da177e4SLinus Torvalds static int 4261da177e4SLinus Torvalds cuda_send_request(struct adb_request *req, int sync) 4271da177e4SLinus Torvalds { 4281da177e4SLinus Torvalds int i; 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds if ((via == NULL) || !cuda_fully_inited) { 4311da177e4SLinus Torvalds req->complete = 1; 4321da177e4SLinus Torvalds return -ENXIO; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds req->reply_expected = 1; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds i = cuda_write(req); 4381da177e4SLinus Torvalds if (i) 4391da177e4SLinus Torvalds return i; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds if (sync) { 4421da177e4SLinus Torvalds while (!req->complete) 4431da177e4SLinus Torvalds cuda_poll(); 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds return 0; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* Enable/disable autopolling */ 4501da177e4SLinus Torvalds static int 4511da177e4SLinus Torvalds cuda_adb_autopoll(int devs) 4521da177e4SLinus Torvalds { 4531da177e4SLinus Torvalds struct adb_request req; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds if ((via == NULL) || !cuda_fully_inited) 4561da177e4SLinus Torvalds return -ENXIO; 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0)); 4591da177e4SLinus Torvalds while (!req.complete) 4601da177e4SLinus Torvalds cuda_poll(); 4611da177e4SLinus Torvalds return 0; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* Reset adb bus - how do we do this?? */ 4651da177e4SLinus Torvalds static int 4661da177e4SLinus Torvalds cuda_reset_adb_bus(void) 4671da177e4SLinus Torvalds { 4681da177e4SLinus Torvalds struct adb_request req; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds if ((via == NULL) || !cuda_fully_inited) 4711da177e4SLinus Torvalds return -ENXIO; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */ 4741da177e4SLinus Torvalds while (!req.complete) 4751da177e4SLinus Torvalds cuda_poll(); 4761da177e4SLinus Torvalds return 0; 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 479523717d1SFinn Thain 4801da177e4SLinus Torvalds /* Construct and send a cuda request */ 4811da177e4SLinus Torvalds int 4821da177e4SLinus Torvalds cuda_request(struct adb_request *req, void (*done)(struct adb_request *), 4831da177e4SLinus Torvalds int nbytes, ...) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds va_list list; 4861da177e4SLinus Torvalds int i; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds if (via == NULL) { 4891da177e4SLinus Torvalds req->complete = 1; 4901da177e4SLinus Torvalds return -ENXIO; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds req->nbytes = nbytes; 4941da177e4SLinus Torvalds req->done = done; 4951da177e4SLinus Torvalds va_start(list, nbytes); 4961da177e4SLinus Torvalds for (i = 0; i < nbytes; ++i) 4971da177e4SLinus Torvalds req->data[i] = va_arg(list, int); 4981da177e4SLinus Torvalds va_end(list); 4991da177e4SLinus Torvalds req->reply_expected = 1; 5001da177e4SLinus Torvalds return cuda_write(req); 5011da177e4SLinus Torvalds } 5024a1b08e8SAnton Blanchard EXPORT_SYMBOL(cuda_request); 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds static int 5051da177e4SLinus Torvalds cuda_write(struct adb_request *req) 5061da177e4SLinus Torvalds { 5071da177e4SLinus Torvalds unsigned long flags; 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) { 5101da177e4SLinus Torvalds req->complete = 1; 5111da177e4SLinus Torvalds return -EINVAL; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds req->next = NULL; 5141da177e4SLinus Torvalds req->sent = 0; 5151da177e4SLinus Torvalds req->complete = 0; 5161da177e4SLinus Torvalds req->reply_len = 0; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds spin_lock_irqsave(&cuda_lock, flags); 5191da177e4SLinus Torvalds if (current_req != 0) { 5201da177e4SLinus Torvalds last_req->next = req; 5211da177e4SLinus Torvalds last_req = req; 5221da177e4SLinus Torvalds } else { 5231da177e4SLinus Torvalds current_req = req; 5241da177e4SLinus Torvalds last_req = req; 5251da177e4SLinus Torvalds if (cuda_state == idle) 5261da177e4SLinus Torvalds cuda_start(); 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds spin_unlock_irqrestore(&cuda_lock, flags); 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds return 0; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds static void 5341da177e4SLinus Torvalds cuda_start(void) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds /* assert cuda_state == idle */ 53706d7e994SFinn Thain if (current_req == NULL) 5381da177e4SLinus Torvalds return; 53997ced1aaSFinn Thain data_index = 0; 540fd7a65a2SFinn Thain if (TREQ_asserted(in_8(&via[B]))) 5411da177e4SLinus Torvalds return; /* a byte is coming in from the CUDA */ 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds /* set the shift register to shift out and send a byte */ 5441da177e4SLinus Torvalds out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT); 54597ced1aaSFinn Thain out_8(&via[SR], current_req->data[data_index++]); 546d23eee88SFinn Thain if (mcu_is_egret) 547d23eee88SFinn Thain assert_TIP_and_TACK(); 548d23eee88SFinn Thain else 549fd7a65a2SFinn Thain assert_TIP(); 5501da177e4SLinus Torvalds cuda_state = sent_first_byte; 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds void 5541da177e4SLinus Torvalds cuda_poll(void) 5551da177e4SLinus Torvalds { 55649f19ce4SOlof Johansson cuda_interrupt(0, NULL); 5571da177e4SLinus Torvalds } 5584a1b08e8SAnton Blanchard EXPORT_SYMBOL(cuda_poll); 5591da177e4SLinus Torvalds 560fe73b582SFinn Thain #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a)) 561fe73b582SFinn Thain 5621da177e4SLinus Torvalds static irqreturn_t 5637d12e780SDavid Howells cuda_interrupt(int irq, void *arg) 5641da177e4SLinus Torvalds { 565ac39452eSFinn Thain unsigned long flags; 566fd7a65a2SFinn Thain u8 status; 5671da177e4SLinus Torvalds struct adb_request *req = NULL; 5681da177e4SLinus Torvalds unsigned char ibuf[16]; 5691da177e4SLinus Torvalds int ibuf_len = 0; 5701da177e4SLinus Torvalds int complete = 0; 5711da177e4SLinus Torvalds 572ac39452eSFinn Thain spin_lock_irqsave(&cuda_lock, flags); 5731da177e4SLinus Torvalds 57418814ee8SFinn Thain /* On powermacs, this handler is registered for the VIA IRQ. But they use 5750251c38cSFinn Thain * just the shift register IRQ -- other VIA interrupt sources are disabled. 5760251c38cSFinn Thain * On m68k macs, the VIA IRQ sources are dispatched individually. Unless 5770251c38cSFinn Thain * we are polling, the shift register IRQ flag has already been cleared. 5780251c38cSFinn Thain */ 5790251c38cSFinn Thain 5800251c38cSFinn Thain #ifdef CONFIG_MAC 5810251c38cSFinn Thain if (!arg) 5820251c38cSFinn Thain #endif 5830251c38cSFinn Thain { 5840251c38cSFinn Thain if ((in_8(&via[IFR]) & SR_INT) == 0) { 585ac39452eSFinn Thain spin_unlock_irqrestore(&cuda_lock, flags); 5861da177e4SLinus Torvalds return IRQ_NONE; 5870251c38cSFinn Thain } else { 5880251c38cSFinn Thain out_8(&via[IFR], SR_INT); 5890251c38cSFinn Thain } 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 592fd7a65a2SFinn Thain status = in_8(&via[B]) & (TIP | TACK | TREQ); 593fd7a65a2SFinn Thain 5941da177e4SLinus Torvalds switch (cuda_state) { 5951da177e4SLinus Torvalds case idle: 596d23eee88SFinn Thain /* System controller has unsolicited data for us */ 5971da177e4SLinus Torvalds (void)in_8(&via[SR]); 598d23eee88SFinn Thain idle_state: 599fd7a65a2SFinn Thain assert_TIP(); 6001da177e4SLinus Torvalds cuda_state = reading; 6011da177e4SLinus Torvalds reply_ptr = cuda_rbuf; 6021da177e4SLinus Torvalds reading_reply = 0; 6031da177e4SLinus Torvalds break; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds case awaiting_reply: 606d23eee88SFinn Thain /* System controller has reply data for us */ 6071da177e4SLinus Torvalds (void)in_8(&via[SR]); 608fd7a65a2SFinn Thain assert_TIP(); 6091da177e4SLinus Torvalds cuda_state = reading; 6101da177e4SLinus Torvalds reply_ptr = current_req->reply; 6111da177e4SLinus Torvalds reading_reply = 1; 6121da177e4SLinus Torvalds break; 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds case sent_first_byte: 615fd7a65a2SFinn Thain if (TREQ_asserted(status)) { 6161da177e4SLinus Torvalds /* collision */ 6171da177e4SLinus Torvalds out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); 6181da177e4SLinus Torvalds (void)in_8(&via[SR]); 619fd7a65a2SFinn Thain negate_TIP_and_TACK(); 6201da177e4SLinus Torvalds cuda_state = idle; 621d23eee88SFinn Thain /* Egret does not raise an "aborted" interrupt */ 622d23eee88SFinn Thain if (mcu_is_egret) 623d23eee88SFinn Thain goto idle_state; 6241da177e4SLinus Torvalds } else { 62597ced1aaSFinn Thain out_8(&via[SR], current_req->data[data_index++]); 626fd7a65a2SFinn Thain toggle_TACK(); 627d23eee88SFinn Thain if (mcu_is_egret) 628d23eee88SFinn Thain assert_TACK(); 6291da177e4SLinus Torvalds cuda_state = sending; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds break; 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds case sending: 6341da177e4SLinus Torvalds req = current_req; 6351da177e4SLinus Torvalds if (data_index >= req->nbytes) { 6361da177e4SLinus Torvalds out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); 6371da177e4SLinus Torvalds (void)in_8(&via[SR]); 638fd7a65a2SFinn Thain negate_TIP_and_TACK(); 6391da177e4SLinus Torvalds req->sent = 1; 6401da177e4SLinus Torvalds if (req->reply_expected) { 6411da177e4SLinus Torvalds cuda_state = awaiting_reply; 6421da177e4SLinus Torvalds } else { 6431da177e4SLinus Torvalds current_req = req->next; 6441da177e4SLinus Torvalds complete = 1; 6451da177e4SLinus Torvalds /* not sure about this */ 6461da177e4SLinus Torvalds cuda_state = idle; 6471da177e4SLinus Torvalds cuda_start(); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds } else { 6501da177e4SLinus Torvalds out_8(&via[SR], req->data[data_index++]); 651fd7a65a2SFinn Thain toggle_TACK(); 652d23eee88SFinn Thain if (mcu_is_egret) 653d23eee88SFinn Thain assert_TACK(); 6541da177e4SLinus Torvalds } 6551da177e4SLinus Torvalds break; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds case reading: 658fe73b582SFinn Thain if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr) 659fe73b582SFinn Thain : ARRAY_FULL(cuda_rbuf, reply_ptr)) 660fe73b582SFinn Thain (void)in_8(&via[SR]); 661fe73b582SFinn Thain else 6621da177e4SLinus Torvalds *reply_ptr++ = in_8(&via[SR]); 663fd7a65a2SFinn Thain if (!TREQ_asserted(status)) { 664d23eee88SFinn Thain if (mcu_is_egret) 665d23eee88SFinn Thain assert_TACK(); 6661da177e4SLinus Torvalds /* that's all folks */ 667fd7a65a2SFinn Thain negate_TIP_and_TACK(); 6681da177e4SLinus Torvalds cuda_state = read_done; 669d23eee88SFinn Thain /* Egret does not raise a "read done" interrupt */ 670d23eee88SFinn Thain if (mcu_is_egret) 671d23eee88SFinn Thain goto read_done_state; 6721da177e4SLinus Torvalds } else { 673fd7a65a2SFinn Thain toggle_TACK(); 674d23eee88SFinn Thain if (mcu_is_egret) 675d23eee88SFinn Thain negate_TACK(); 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds break; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds case read_done: 6801da177e4SLinus Torvalds (void)in_8(&via[SR]); 681d23eee88SFinn Thain read_done_state: 6821da177e4SLinus Torvalds if (reading_reply) { 6831da177e4SLinus Torvalds req = current_req; 6841da177e4SLinus Torvalds req->reply_len = reply_ptr - req->reply; 6851da177e4SLinus Torvalds if (req->data[0] == ADB_PACKET) { 6861da177e4SLinus Torvalds /* Have to adjust the reply from ADB commands */ 6871da177e4SLinus Torvalds if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) { 6881da177e4SLinus Torvalds /* the 0x2 bit indicates no response */ 6891da177e4SLinus Torvalds req->reply_len = 0; 6901da177e4SLinus Torvalds } else { 6911da177e4SLinus Torvalds /* leave just the command and result bytes in the reply */ 6921da177e4SLinus Torvalds req->reply_len -= 2; 6931da177e4SLinus Torvalds memmove(req->reply, req->reply + 2, req->reply_len); 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds current_req = req->next; 6971da177e4SLinus Torvalds complete = 1; 698cfbf9980SFinn Thain reading_reply = 0; 6991da177e4SLinus Torvalds } else { 7001da177e4SLinus Torvalds /* This is tricky. We must break the spinlock to call 7011da177e4SLinus Torvalds * cuda_input. However, doing so means we might get 7021da177e4SLinus Torvalds * re-entered from another CPU getting an interrupt 7031da177e4SLinus Torvalds * or calling cuda_poll(). I ended up using the stack 7041da177e4SLinus Torvalds * (it's only for 16 bytes) and moving the actual 7051da177e4SLinus Torvalds * call to cuda_input to outside of the lock. 7061da177e4SLinus Torvalds */ 7071da177e4SLinus Torvalds ibuf_len = reply_ptr - cuda_rbuf; 7081da177e4SLinus Torvalds memcpy(ibuf, cuda_rbuf, ibuf_len); 7091da177e4SLinus Torvalds } 710cfbf9980SFinn Thain reply_ptr = cuda_rbuf; 7111da177e4SLinus Torvalds cuda_state = idle; 7121da177e4SLinus Torvalds cuda_start(); 713a6466243SFinn Thain if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) { 714a6466243SFinn Thain assert_TIP(); 715a6466243SFinn Thain cuda_state = reading; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds break; 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds default: 720523717d1SFinn Thain pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state); 7211da177e4SLinus Torvalds } 722ac39452eSFinn Thain spin_unlock_irqrestore(&cuda_lock, flags); 7231da177e4SLinus Torvalds if (complete && req) { 7241da177e4SLinus Torvalds void (*done)(struct adb_request *) = req->done; 7251da177e4SLinus Torvalds mb(); 7261da177e4SLinus Torvalds req->complete = 1; 7271da177e4SLinus Torvalds /* Here, we assume that if the request has a done member, the 7281da177e4SLinus Torvalds * struct request will survive to setting req->complete to 1 7291da177e4SLinus Torvalds */ 7301da177e4SLinus Torvalds if (done) 7311da177e4SLinus Torvalds (*done)(req); 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds if (ibuf_len) 7347d12e780SDavid Howells cuda_input(ibuf, ibuf_len); 7351da177e4SLinus Torvalds return IRQ_HANDLED; 7361da177e4SLinus Torvalds } 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds static void 7397d12e780SDavid Howells cuda_input(unsigned char *buf, int nb) 7401da177e4SLinus Torvalds { 7411da177e4SLinus Torvalds switch (buf[0]) { 7421da177e4SLinus Torvalds case ADB_PACKET: 7431da177e4SLinus Torvalds #ifdef CONFIG_XMON 7441da177e4SLinus Torvalds if (nb == 5 && buf[2] == 0x2c) { 7451da177e4SLinus Torvalds extern int xmon_wants_key, xmon_adb_keycode; 7461da177e4SLinus Torvalds if (xmon_wants_key) { 7471da177e4SLinus Torvalds xmon_adb_keycode = buf[3]; 7481da177e4SLinus Torvalds return; 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds #endif /* CONFIG_XMON */ 7521da177e4SLinus Torvalds #ifdef CONFIG_ADB 7537d12e780SDavid Howells adb_input(buf+2, nb-2, buf[1] & 0x40); 7541da177e4SLinus Torvalds #endif /* CONFIG_ADB */ 7551da177e4SLinus Torvalds break; 7561da177e4SLinus Torvalds 757d23eee88SFinn Thain case TIMER_PACKET: 758d23eee88SFinn Thain /* Egret sends these periodically. Might be useful as a 'heartbeat' 759d23eee88SFinn Thain * to trigger a recovery for the VIA shift register errata. 760d23eee88SFinn Thain */ 761d23eee88SFinn Thain break; 762d23eee88SFinn Thain 7631da177e4SLinus Torvalds default: 764523717d1SFinn Thain print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1, 765523717d1SFinn Thain buf, nb, false); 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds } 768