1 /* $NetBSD: adc.c,v 1.3 2003/10/22 23:52:46 uwe Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Valeriy E. Ushakov 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: adc.c,v 1.3 2003/10/22 23:52:46 uwe Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/device.h> 36 #include <sys/malloc.h> 37 #include <sys/systm.h> 38 #ifdef GPROF 39 #include <sys/gmon.h> 40 #endif 41 42 #include <sh3/adcreg.h> 43 #include <sh3/dev/adcvar.h> 44 45 #define ADC_(x) (*((volatile uint8_t *)SH7709_AD ## x)) 46 47 48 struct adc_softc { 49 struct device sc_dev; 50 }; 51 52 static int adc_match(struct device *, struct cfdata *, void *); 53 static void adc_attach(struct device *, struct device *, void *); 54 55 CFATTACH_DECL(adc, sizeof(struct adc_softc), 56 adc_match, adc_attach, NULL, NULL); 57 58 static int adc_search(struct device *, struct cfdata *, void *); 59 static int adc_print(void *, const char *); 60 61 62 static int 63 adc_match(struct device *parent, struct cfdata *cfp, void *aux) 64 { 65 66 /* REMINDER: also in 7727 and 7729 */ 67 if ((cpu_product != CPU_PRODUCT_7709) 68 && (cpu_product != CPU_PRODUCT_7709A)) 69 return (0); 70 71 if (strcmp(cfp->cf_name, "adc") != 0) 72 return (0); 73 74 return (1); 75 } 76 77 78 static void 79 adc_attach(struct device *parent, struct device *self, void *aux) 80 { 81 /* struct adc_softc *sc = (struct adc_softc *)self; */ 82 83 ADC_(CSR) = 0; 84 ADC_(CR) = 0; 85 86 printf("\n"); 87 config_search(adc_search, self, NULL); 88 } 89 90 91 static int 92 adc_search(struct device *parent, struct cfdata *cf, void *aux) 93 { 94 95 if (config_match(parent, cf, NULL) > 0) 96 config_attach(parent, cf, NULL, adc_print); 97 98 return (0); 99 } 100 101 102 static int 103 adc_print(void *aux, const char *pnp) 104 { 105 106 return (pnp ? QUIET : UNCONF); 107 } 108 109 110 /* 111 * Sample specified ADC channel. 112 * Must be called at spltty(). 113 */ 114 int 115 adc_sample_channel(int chan) 116 { 117 volatile uint8_t *hireg; 118 volatile uint8_t *loreg; 119 int regoff; 120 uint8_t csr; 121 int timo; 122 #ifdef DIAGNOSTIC 123 uint8_t cr; 124 char bits[128]; 125 #endif 126 if ((chan < 0) || (chan >= 8)) 127 return (-1); 128 129 regoff = (chan & 0x03) << 2; 130 hireg = (volatile uint8_t *)(SH7709_ADDRAH + regoff); 131 loreg = (volatile uint8_t *)(SH7709_ADDRAL + regoff); 132 133 /* the loop below typically takes 27 iterations on j680 */ 134 timo = 300; 135 136 #ifdef DIAGNOSTIC 137 csr = ADC_(CSR); 138 if ((csr & SH7709_ADCSR_ADST) != 0) { 139 /* another conversion is in progress?! */ 140 printf("adc_sample_channel(%d): CSR=%s", chan, 141 bitmask_snprintf(csr, SH7709_ADCSR_BITS, 142 bits, sizeof(bits))); 143 cr = ADC_(CR); 144 cr &= ~0x07; /* three lower bits always read as 1s */ 145 printf(", CR=%s\n", 146 bitmask_snprintf(cr, SH7709_ADCR_BITS, 147 bits, sizeof(bits))); 148 return (-1); 149 } 150 #endif 151 152 /* start scanning */ 153 ADC_(CSR) = chan | SH7709_ADCSR_ADST | SH7709_ADCSR_CKS; 154 155 do { 156 csr = ADC_(CSR); 157 if (timo-- == 0) 158 break; 159 } while ((csr & SH7709_ADCSR_ADF) == 0); 160 161 /* stop scanning */ 162 csr &= ~(SH7709_ADCSR_ADF | SH7709_ADCSR_ADST); 163 ADC_(CSR) = csr; 164 165 if (timo <= 0) { 166 printf("adc_sample_channel(%d): timed out\n", chan); 167 return (-1); 168 } 169 170 /* 171 * 10 bit of data: bits [9..2] in the high register, bits 172 * [1..0] in the bits [7..6] of the low register. 173 */ 174 return (((*hireg << 8) | *loreg) >> 6); 175 } 176