Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Driver for the ADB controller in the Mac I/O (Hydra) chip.
0004  */
0005 #include <linux/types.h>
0006 #include <linux/errno.h>
0007 #include <linux/kernel.h>
0008 #include <linux/delay.h>
0009 #include <linux/spinlock.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/pgtable.h>
0012 #include <linux/of.h>
0013 #include <linux/of_address.h>
0014 #include <linux/of_irq.h>
0015 #include <linux/adb.h>
0016 
0017 #include <asm/io.h>
0018 #include <asm/hydra.h>
0019 #include <asm/irq.h>
0020 #include <linux/init.h>
0021 #include <linux/ioport.h>
0022 
0023 struct preg {
0024     unsigned char r;
0025     char pad[15];
0026 };
0027 
0028 struct adb_regs {
0029     struct preg intr;
0030     struct preg data[9];
0031     struct preg intr_enb;
0032     struct preg dcount;
0033     struct preg error;
0034     struct preg ctrl;
0035     struct preg autopoll;
0036     struct preg active_hi;
0037     struct preg active_lo;
0038     struct preg test;
0039 };
0040 
0041 /* Bits in intr and intr_enb registers */
0042 #define DFB 1       /* data from bus */
0043 #define TAG 2       /* transfer access grant */
0044 
0045 /* Bits in dcount register */
0046 #define HMB 0x0f        /* how many bytes */
0047 #define APD 0x10        /* auto-poll data */
0048 
0049 /* Bits in error register */
0050 #define NRE 1       /* no response error */
0051 #define DLE 2       /* data lost error */
0052 
0053 /* Bits in ctrl register */
0054 #define TAR 1       /* transfer access request */
0055 #define DTB 2       /* data to bus */
0056 #define CRE 4       /* command response expected */
0057 #define ADB_RST 8       /* ADB reset */
0058 
0059 /* Bits in autopoll register */
0060 #define APE 1       /* autopoll enable */
0061 
0062 static volatile struct adb_regs __iomem *adb;
0063 static struct adb_request *current_req, *last_req;
0064 static DEFINE_SPINLOCK(macio_lock);
0065 
0066 static int macio_probe(void);
0067 static int macio_init(void);
0068 static irqreturn_t macio_adb_interrupt(int irq, void *arg);
0069 static int macio_send_request(struct adb_request *req, int sync);
0070 static int macio_adb_autopoll(int devs);
0071 static void macio_adb_poll(void);
0072 static int macio_adb_reset_bus(void);
0073 
0074 struct adb_driver macio_adb_driver = {
0075     .name         = "MACIO",
0076     .probe        = macio_probe,
0077     .init         = macio_init,
0078     .send_request = macio_send_request,
0079     .autopoll     = macio_adb_autopoll,
0080     .poll         = macio_adb_poll,
0081     .reset_bus    = macio_adb_reset_bus,
0082 };
0083 
0084 int macio_probe(void)
0085 {
0086     struct device_node *np;
0087 
0088     np = of_find_compatible_node(NULL, "adb", "chrp,adb0");
0089     if (np) {
0090         of_node_put(np);
0091         return 0;
0092     }
0093     return -ENODEV;
0094 }
0095 
0096 int macio_init(void)
0097 {
0098     struct device_node *adbs;
0099     struct resource r;
0100     unsigned int irq;
0101 
0102     adbs = of_find_compatible_node(NULL, "adb", "chrp,adb0");
0103     if (adbs == 0)
0104         return -ENXIO;
0105 
0106     if (of_address_to_resource(adbs, 0, &r)) {
0107         of_node_put(adbs);
0108         return -ENXIO;
0109     }
0110     adb = ioremap(r.start, sizeof(struct adb_regs));
0111 
0112     out_8(&adb->ctrl.r, 0);
0113     out_8(&adb->intr.r, 0);
0114     out_8(&adb->error.r, 0);
0115     out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
0116     out_8(&adb->active_lo.r, 0xff);
0117     out_8(&adb->autopoll.r, APE);
0118 
0119     irq = irq_of_parse_and_map(adbs, 0);
0120     of_node_put(adbs);
0121     if (request_irq(irq, macio_adb_interrupt, 0, "ADB", (void *)0)) {
0122         printk(KERN_ERR "ADB: can't get irq %d\n", irq);
0123         return -EAGAIN;
0124     }
0125     out_8(&adb->intr_enb.r, DFB | TAG);
0126 
0127     printk("adb: mac-io driver 1.0 for unified ADB\n");
0128 
0129     return 0;
0130 }
0131 
0132 static int macio_adb_autopoll(int devs)
0133 {
0134     unsigned long flags;
0135     
0136     spin_lock_irqsave(&macio_lock, flags);
0137     out_8(&adb->active_hi.r, devs >> 8);
0138     out_8(&adb->active_lo.r, devs);
0139     out_8(&adb->autopoll.r, devs? APE: 0);
0140     spin_unlock_irqrestore(&macio_lock, flags);
0141     return 0;
0142 }
0143 
0144 static int macio_adb_reset_bus(void)
0145 {
0146     unsigned long flags;
0147     int timeout = 1000000;
0148 
0149     /* Hrm... we may want to not lock interrupts for so
0150      * long ... oh well, who uses that chip anyway ? :)
0151      * That function will be seldom used during boot
0152      * on rare machines, so...
0153      */
0154     spin_lock_irqsave(&macio_lock, flags);
0155     out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
0156     while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
0157         if (--timeout == 0) {
0158             out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
0159             spin_unlock_irqrestore(&macio_lock, flags);
0160             return -1;
0161         }
0162     }
0163     spin_unlock_irqrestore(&macio_lock, flags);
0164     return 0;
0165 }
0166 
0167 /* Send an ADB command */
0168 static int macio_send_request(struct adb_request *req, int sync)
0169 {
0170     unsigned long flags;
0171     int i;
0172     
0173     if (req->data[0] != ADB_PACKET)
0174         return -EINVAL;
0175     
0176     for (i = 0; i < req->nbytes - 1; ++i)
0177         req->data[i] = req->data[i+1];
0178     --req->nbytes;
0179     
0180     req->next = NULL;
0181     req->sent = 0;
0182     req->complete = 0;
0183     req->reply_len = 0;
0184 
0185     spin_lock_irqsave(&macio_lock, flags);
0186     if (current_req != 0) {
0187         last_req->next = req;
0188         last_req = req;
0189     } else {
0190         current_req = last_req = req;
0191         out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
0192     }
0193     spin_unlock_irqrestore(&macio_lock, flags);
0194     
0195     if (sync) {
0196         while (!req->complete)
0197             macio_adb_poll();
0198     }
0199 
0200     return 0;
0201 }
0202 
0203 static irqreturn_t macio_adb_interrupt(int irq, void *arg)
0204 {
0205     int i, n, err;
0206     struct adb_request *req = NULL;
0207     unsigned char ibuf[16];
0208     int ibuf_len = 0;
0209     int complete = 0;
0210     int autopoll = 0;
0211     int handled = 0;
0212 
0213     spin_lock(&macio_lock);
0214     if (in_8(&adb->intr.r) & TAG) {
0215         handled = 1;
0216         if ((req = current_req) != 0) {
0217             /* put the current request in */
0218             for (i = 0; i < req->nbytes; ++i)
0219                 out_8(&adb->data[i].r, req->data[i]);
0220             out_8(&adb->dcount.r, req->nbytes & HMB);
0221             req->sent = 1;
0222             if (req->reply_expected) {
0223                 out_8(&adb->ctrl.r, DTB + CRE);
0224             } else {
0225                 out_8(&adb->ctrl.r, DTB);
0226                 current_req = req->next;
0227                 complete = 1;
0228                 if (current_req)
0229                     out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
0230             }
0231         }
0232         out_8(&adb->intr.r, 0);
0233     }
0234 
0235     if (in_8(&adb->intr.r) & DFB) {
0236         handled = 1;
0237         err = in_8(&adb->error.r);
0238         if (current_req && current_req->sent) {
0239             /* this is the response to a command */
0240             req = current_req;
0241             if (err == 0) {
0242                 req->reply_len = in_8(&adb->dcount.r) & HMB;
0243                 for (i = 0; i < req->reply_len; ++i)
0244                     req->reply[i] = in_8(&adb->data[i].r);
0245             }
0246             current_req = req->next;
0247             complete = 1;
0248             if (current_req)
0249                 out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
0250         } else if (err == 0) {
0251             /* autopoll data */
0252             n = in_8(&adb->dcount.r) & HMB;
0253             for (i = 0; i < n; ++i)
0254                 ibuf[i] = in_8(&adb->data[i].r);
0255             ibuf_len = n;
0256             autopoll = (in_8(&adb->dcount.r) & APD) != 0;
0257         }
0258         out_8(&adb->error.r, 0);
0259         out_8(&adb->intr.r, 0);
0260     }
0261     spin_unlock(&macio_lock);
0262     if (complete && req) {
0263         void (*done)(struct adb_request *) = req->done;
0264         mb();
0265         req->complete = 1;
0266         /* Here, we assume that if the request has a done member, the
0267              * struct request will survive to setting req->complete to 1
0268          */
0269         if (done)
0270         (*done)(req);
0271     }
0272     if (ibuf_len)
0273         adb_input(ibuf, ibuf_len, autopoll);
0274 
0275     return IRQ_RETVAL(handled);
0276 }
0277 
0278 static void macio_adb_poll(void)
0279 {
0280     unsigned long flags;
0281 
0282     local_irq_save(flags);
0283     if (in_8(&adb->intr.r) != 0)
0284         macio_adb_interrupt(0, NULL);
0285     local_irq_restore(flags);
0286 }